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.
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"?
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.
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.
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
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:
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];
constint 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).
@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.