1

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.

MistFTW
  • 79
  • 1
  • 1
  • 9
  • 1
    You are looking for `std::vector`, which has `size()` and `push_back()` members. – Nemo May 19 '15 at 04:40
  • To what end? Why are you doing this? It's possible that a different collection (such as a vector) may be more suitable for your needs. – Luke May 19 '15 at 04:41
  • @Nemo: Sorry, we haven't covered vectors in class yet so I don't want to get too ahead. This is just some practice with arrays I came up with. – MistFTW May 19 '15 at 04:42
  • 2
    @MistFTW: Then use a second variable to keep track of the first unused "slot" in the array. There is no such thing as an "empty integer" in general. – Nemo May 19 '15 at 04:45
  • 2
    @MistFTW: You've created an array of 5 `int` and all 5 elements in the array exist. There's no such thing as an empty or non-existent element. You'll need to keep track of how many elements should be considered valid. – Blastfurnace May 19 '15 at 04:45

3 Answers3

5

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

Jeet Parekh
  • 740
  • 2
  • 8
  • 25
3

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;
}
Community
  • 1
  • 1
Jun
  • 764
  • 5
  • 8
1

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;
Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86
  • 1
    Assuming you never want to actually store the magic value (0) in the array! – Luke May 19 '15 at 04:41
  • I'm still new to C++, so if you don't mind explaining, what exactly does adding `{0}` do? – MistFTW May 19 '15 at 04:42
  • http://stackoverflow.com/questions/5591492/c-c-array-initialization-with-0-0 Quote: They are equivalent regarding the generated code (at least in optimised builds) because when an array is initialised with {0} syntax, all values that are not explicitly specified are implicitly initialised with 0, and the compiler will know enough to insert a call to memset. – Jun May 19 '15 at 04:44