I have an array:
int array[5];
And I am going to use this array to put some values in it. Now my question: how do I check whether something has been filled in the array at place i after filling in some numbers? Thank you
I have an array:
int array[5];
And I am going to use this array to put some values in it. Now my question: how do I check whether something has been filled in the array at place i after filling in some numbers? Thank you
The array contains values from the moment it is instantiated, so you cannot query it to check whether it has been "filled". But you could keep a separate data structure to keep track of which elements you have filled. Or you could wrap the array and the other data structure in a class that allows you to assign values to the array and check whether they have already been assigned.
You can use an other array saving if case 'i' has been filled or not.
int array[5]
bool filled[5]={false};
array[0] = 42;
filled[0] = true;
But it's quite dirty.
int array. It's a waste of memory. Some "bitmap" could be used.uint8_t and use bitwise operations, to check each bit. In other words, encode 8 elements in 1B (1 per each bit).char is a "byte", but the standard uses that word to mean a unit of storage the size of a char not a unit of storage 8 bits in length.well you must ask if your array in position "i" is ==NULL something like this
int array[5];
int main()
{
array[0]=1;
array[1]=2;
array[3]=1;
for(int i =0; i < 6;i++)
{
if(array[i] != NULL)
{
cout<<"Value is: "<<array[i]<<endl;
}else{
cout<<"The Value is NULL"<<endl;
}
}
return 0;
}
NULL is just define of 0. There could be a value 0 in the arrey, which may be valid.