how to find the actual size of int array?

my problem is that i want to know how many elements of an int array are initialized with values.
for eg:-
int arr[50]={1,0,2,9,3};
As u can see the above var 'arr' has 50 int spaces but it is only give 5 values.
so how to make a program to print the no. of elements initialized with values, that is 5???
guys i hav tried following method:
int i;
int arr[50]={1,2,8,2,3};
whle(arr[i])
i++;
cout<<i;
But this is successful ONLY if our array elements DOESNOT have a zero value.
Last edited on
The other elements are also "initialized".
They do not contain "nothing".
You can of course initialized the remaining values to a "magic value" but i'd not suggest it.

If you switch to a std::vector there is a clean way to find out.
The short answer is: No, there is no way to find out how many elements were initialized. But that should be no impediment. I mean, clearly the number will never change because it is a hardcoded array. So why the need to know this "programatically"?
closed account (zb0S216C)
All elements of an array (and every other variable type for that matter) are given unused memory if you don't provide a value at the point of declaration. This effectively initializes the variable or array element.

Wazzak
but in case of char array we can easily find the string length, then why not in case of int?
Arrays of chars have a special convention: An array of chars will be ended with a null char to indicate its end. This convention (or any other convention) does not exist for arrays of ints or other types of arrays, except maybe for arrays of wchar_t's, which are equivalent to arrays of chars.
closed account (zb0S216C)
Finding the length of any non-dynamically allocated array is easy. You simply write:

1
2
3
char Array[ ] = { ... };

unsigned int Array_Length( sizeof( Array ) / sizeof( Array[ 0 ] ) );

Wazzak
Yes Framework, but the OP is allocating 50 elements, but only initializing 5. I haven't tested it, but I would say your math yields 50, not 5.
closed account (zwA4jE8b)
besides using a sentinel value, I also do not see a way to accomplish this.
A way to keep track of home many values are initialized would be to use a counter variable.
counter++ //every time an array slot is initialized
counter-- //every time and array slot is deleted
closed account (zb0S216C)
Taken WebJose's post into account, I can only think of 1 way to determine if the element is initialized or not: check if the element's value is within a given range. If it's not, then it's uninitialized. For example:

1
2
3
4
5
6
7
typedef unsigned int OFFSET;

int Array[ 50 ] = { ... };

for( OFFSET Offset( 0 ); Offset < 50; Offset++ )
    if( ( Array[ Offset ] >= 0 ) && ( Array[ Offset ] <= 10 ) )
        std::cout << "Array[" << Offset << "] is initialized" << std::endl;

Wazzak
couldn't you create a copy array of the original array when first declared? and set each element of the copy array to the initial (that is, pre-initialized) values of the original array, and later to check to see if the array has been initialized check each element to see if it matches the element in copy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Array[10];

const int Copy[] = {Array[0], Array[1], Array[2], Array[3], Array[4], Array[5], Array[6], Array[7], Array[8], Array[9], Array[10]};

int size()
{
    int i;
    for (i = 0; i < 10; ++i)
        {
            if (Array[i] == Copy[i])
                break;
        }
    return i;
}


edit:
I guess it works better if not declaring global variables (which are in my compiler all initialized to zero anyway).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int size(int Array[], const int Copy [])
{
    int i;
    for (i = 0; i < 10; ++i)
        {
            if (Array[i] == Copy[i])
                break;
        }
    return i;
}

int main()
{
    int Array[10];
    const int Copy[] = {Array[0], Array[1], Array[2], Array[3], Array[4], Array[5], Array[6], Array[7], Array[8], Array[9], Array[10]};
    cout << "Enter your age: \n>" << flush;
    int age; cin >> Array[0];
    cout << "Enter your size. \n>" << flush;
    cin >> Array[1];
    cout << "now size = " << size(Array, Copy) << ".\n" << flush;
    cout << "Array[2] = " << Array[2] << endl;
}
Last edited on
@Framework: Interesting, but how can you ensure that junk in RAM doesn't fall within the range? Because junk can be anything, including potential within-the-range values.

To me the bottom line is: Since you are hardcoding the initial values, hardcode the number of initialized members too, or define a sentinel value, like zero or -1 and work it out equivalently to arrays of chars.
Topic archived. No new replies allowed.