MistFTW
MistFTW

Reputation: 79

Checking if an index of an array is empty

I'm currently trying to write a script so that I can add an item to the last index the array has an item in. For example, if I initialized an array int a[5] and a[0], a[1], a[2] all have something, then the integer would be added to a[3]Here is what I have :

int main(){
    int a[5];
    a[0] = 10;
    a[1] = 20;
    a[2] = 30;
    for (int i = 0; i < 5; i++){
        if (a[i] < 0){
            a[i] = 40; //Just an example for what it would be like.
        }
    }
}

I can't help but feel that there is a better way to do this, maybe a different if condition. I want to know if there's another way to check if the next index is empty.

Upvotes: 1

Views: 14610

Answers (3)

Jun
Jun

Reputation: 804

A few things to probably clear up what looks like a misunderstanding around what an array is:

When you declare an array say

int main()
{
    int a[5];

    for (int i = 0; i < 5; i++)
    {
        printf("a[%d] = %d", i, a[i]);
    }
}

All elements in the array exist already. Namely, you can access a[0] ... a[4] without hitting an error. All values of the array have already been set implicitly and you can see this by seeing the output of the printf. Note that those are values that you haven't set yourself and will vary. If you're curious about why they vary, you can see this: Variable initialization in C++

To set those values explicitly, you can initialize all values in the array to 0 like so:

int main()
{
    int a[5] = {0};

    for (int i = 0; i < 5; i++)
    {
        printf("a[%d] = %d", i, a[i]);
    }
}

or through use of a static initializer

int main()
{
    int a[5] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++)
    {
        printf("a[%d] = %d", i, a[i]);
    }
}

However because all values of the array already exist on creation, there isn't really such a state as "uninitialized array" in C++ as they are . The value of a[3] is either set implicitly or explicitly depending on how you created the array.

std::vector is a dynamically growing array, based on how much space you need. In order to have this effect, std::vector keeps track of how much of the array is "used" through use of a size variable. If you wanted to reimplement that to get an idea of how it might be done, you would probably want a class like:

class MyArray
{
public:
    MyArray() : m_size(0)
    {
    }

    void AddVal(int data)
    {
        if (m_size < 5)
        {
            m_array[m_size++] = data;
        }
    }

    int GetSize()
    {
        return m_size;
    }

private:
    int m_array[5];
    int m_size;
}

Upvotes: 3

Jeet Parekh
Jeet Parekh

Reputation: 740

You could use an array index counter. Say, int counter = 0;

Use the counter as an index when you store integers to the array a, like a[counter] = 5 After you add an integer to your array, increment the counter, counter++.

This way you could make sure that the next value being added to the array is always added the way you described in the question

Upvotes: 5

Sara Fuerst
Sara Fuerst

Reputation: 6068

If you initialize the array to 0, you can check if the value is 0.

Initilize:

int array[5] = {0};

Check for 0:

array[4] == 0;

Upvotes: 1

Related Questions