1

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;
}
Deanie
  • 2,316
  • 2
  • 19
  • 35
Abhineet
  • 6,459
  • 10
  • 35
  • 53

5 Answers5

4

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Is there any other way to judge which location consist garbage value? – Abhineet Jul 16 '11 at 11:29
  • 2
    No. There's two ways that array will be initialized, or not. When you declare it, either it contains random garbage, or it contains a common constant, typically 0 (since it is an *int* array.) Either way, you yourself has to track whether you've set an element or not. There is no flag, or bit, or whatever that can tell you whether you have set an array element or not. If the array elements are initialized to 0 upon declaration, and you must check to ensure (and I mean check the compiler documentation, not what *happens* to happen right now), then you can use 0 as your magic value. – Lasse V. Karlsen Jul 16 '11 at 11:34
  • 1
    This older Stackoverflow thread, [How to initialize an array in C](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c/201116#201116), has a good discussion of setting all entries of an array to the same value (easiest if zero is your "magic" value). – hardmath Jul 16 '11 at 11:36
  • 1
    @Abhineet if you can't trust magic values, and if you can set array elements in a sparse way, then you need a "parallel" array, say `bool as[10] = { false }`, and you have to consult it to know where `a` contains a valid int (and when you write a valid int into `a`, you must update `as`) – ShinTakezou Jul 16 '11 at 11:45
1

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.)

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
1

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);
cendar
  • 34
  • 3
-1

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);

       }
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43
-1

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.

Dori
  • 915
  • 1
  • 12
  • 20
techno
  • 6,100
  • 16
  • 86
  • 192
  • This works fine if i declare array variable as `int a[10] = {};` or `int a[10] = {1,2,3}` etc. It doesn't work correctly if i declare variable as `int a[10];` and not giving the input or `int *a = (int*)malloc(sizeof(int)*10);` . In many scenarios it doesn't work. – Abhineet Jul 18 '11 at 11:51
  • Also if i give `0` as a user input for a particular location then the result is wrong. Leave it, i know there is no way to find the difference b/w user input & garbage value. – Abhineet Jul 18 '11 at 11:59