I have an array variable, say for an example:-
int a[10];
And suppose i have added 4 elements say 1,2,3,4
starting from a[0]
till a[3]
.
Now how do i find that how much element is present in the array variable a
whose max size is 10
?
My way to obtain the number of elements present in the array assuming that user will give input greater than or equal to Zero:-
#include<iostream>
using namespace std;
#define SIZE 10
int main(){
int *a = (int*)malloc(sizeof(int)*SIZE);
int iCount=0;
int iNumberOfElements,iElements;
cout<<"\nEnter the number of elements to be entered:";
cin>>iNumberOfElements;
cout<<"\nEnter the elements(Please enter elements greater than or equal to zero):\n";
for (int jIndex = 0; jIndex < iNumberOfElements; jIndex++){
cin>>iElements;
while(iElements<0){
cout<<"\nPlease enter the element greater than or equal to Zero.\nRe-enter:\n";
cin>>iElements;
}
a[jIndex] = iElements;
}
for(int iIndex=0;iIndex<SIZE;iIndex++){
if(a[iIndex] >= 0){ //I am checking in this way assuming that user will give input >= 0
iCount++;
}
}
cout<<"\nThe total number of element present in the array is:"<<iCount<<endl;
return 0;
}
You have to build in that convention yourself.
For instance, you could initialize the whole array to some magic constant that signals "not in use", or you could track the length through a separate variable.
Either way, the framework will not help you at all, there is no way. For all intents and purposes, the array has 10 elements, whether you set them or not.
By
int a[10];
You've allocated space for 10 ints with indeterminate values (or 0's if the array has static storage duration). When you say
i have added 4 elements say 1,2,3,4 starting from a[0] till a[3]
You probably mean you've assigned values to elements. So, they aren't marked in any other way.
You could set all elements to -1
so that you could determine whether you've assigned to them, but be careful - you can't know where your array ends! (It is only possible in C++ by using function templates and passing the array as a reference to an array.)
Without using a counter or to set the table with a default value,
it can be done if the elements are added or deleted step by step by using the address of the table in a pointer and then calculate the difference between the two :
int a[10],*ptr=a;
*ptr=1;
*++ptr=2;
*++ptr=3;
*++ptr=4;
printf("start addr : %p\ncurrent addr %p\n",
a,ptr);
printf("present in a : %d rest : %d\n",ptr-a+1,&a[9]-ptr);
you can define a static or volatile variable that keep tracking how many number present in array or initialized.
in following program j keep track of numbers in array a
int main()
{
int a[10];
static int j=0;
do{
scanf("%d",&a[i]);
if(a[i]>=0)
j++;
}while(a[i++]>=0);
}
The Number of elements in an array can be calculated by using a simple loop
int i=0,count=0;
for(i=0;i<10;i++)
{
if(a[i]!='/0')
{
count++;
}
}
Here i is an integer variable,count is used for counting the number of elements.The value '10' is used assuming that the maximum size of the array is 10.If only enter 4 elements to an array of size 10,this Code will return count 4.