0

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

1
  • 1
    Initialise with a known value and compare whether it's different, surely? Commented Aug 2, 2012 at 13:16

5 Answers 5

5

There's no such "elegant" way.

The only way to do this is to have some special value, that will indicate "not filled". For example,

int array[5] = { -1, -1, -1, -1, -1 };

The other option is to have a bitmap, which will tell you which positions are filled, which - not.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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.

5 Comments

There's no need from another int array. It's a waste of memory. Some "bitmap" could be used.
Of course, I edited using boolean (better), but how would you use a bitmap ? I mean, what data structure would you use ? A integer array eight times smaller ?
I'd use an array with some fixed size type. For example, array from uint8_t and use bitwise operations, to check each bit. In other words, encode 8 elements in 1B (1 per each bit).
Yes, I wanted to say a char array eights time smaller. But it's the same with uint8_t.
@JérômeBoé It is better to use a type that expresses it's actual length in this case. Yes a char is an octet on every consumer platform, but the standard does not guarantee or require this. A 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.
0

as it's C++ why not use a container of std::pair?

one element of the pair is a boolean (true if value is set), and the other one is the needed value.

Comments

-1

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

2 Comments

NULL is just define of 0. There could be a value 0 in the arrey, which may be valid.
He can iterate through it, right, but what else? Nothing. Does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.