-2

i am trying to access an array elements and make some Logic gates operations over it, so basically i am trying to get the neighbors of each element, but i am getting the error, Index was outside the bounds of the array error.. thats one of my conditional if statements, and i know i am trying to access an element which is not exist either less than 0 element position or bigger than or = array.length

 public static int[] cellCompete(int[] states, int days)
    {
        int counter = 0;
        // INSERT YOUR CODE HERE
        while (counter <= days)
        {
            for (int i = 0; i < states.Length; i++)
            {

                if(i < states.Length - 1 || i > states[0])
                {
                    if (states[0] == 0 && states[i + 1] == 0)
                    {
                        states[i - 1] = 0;
                    }
                    if (states[0] == 0 && states[i + 1] == 1)
                    {
                        states[i - 1] = 1;
                    }
                    if (states[i] == states.Length - 1 && states[i - 1] == 0)
                    {
                        states[i] = 0;
                    }
                    if (states[i] == states.Length - 1 && states[i - 1] == 1)
                    {
                        states[i] = 1;
                    }
                    if (states[i + 1] == 0 && states[i - 1] == 0)
                    {
                        states[i] = 0;
                    }
                    if (states[i + 1] == 0 && states[i - 1] == 1)
                    {
                        states[i] = 1;
                    }
                    if (states[i + 1] == 1 && states[i - 1] == 0)
                    {
                        states[i] = 1;
                    }
                    if (states[i + 1] == 1 && states[i - 1] == 1)
                    {
                        states[i] = 0;
                    }
                }
            }
            counter++;
        }
        return states;
    }

any idea how to fix it?

Wael Elsayegh
  • 16
  • 1
  • 6

3 Answers3

0

If you do this in a loop, ensure i is not < 0 and never > than "array size - 1"

then you can safely use i as array index

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

I'm assuming that you're doing this work inside a loop such that it looks something like:

for (int i = 0; i < states.Length; i++)
{
    if (states[i] == 0 && states[i + 1] == 0)
     {
        states[i] = 0;
     }
}

If so, and what you're trying to do is compare pairs of things from within an array then the conventional way to do this is to cache the first one and then loop from 1 -> n-1. e.g.:

var previousState = states[0];
for (int i = 1; i < states.Length; i++)
{
    if (previousState == 0 && states[i] == 0)
    {
        states[i -1] = 0;
    }
}
Ross Gurbutt
  • 969
  • 1
  • 8
  • 15
0

When you declare an array, you specify the size. That is, the maximum number of items it can hold. So,

int[] states = new int[2];

means the states array can hold up to 2 items. Therefore following will be valid and won't throw the above exception:

var one = states[0];
var two = states[1];

But this will, because it can only hold 2 items max but you're trying to access a third item.

var three = states[2];

So in your loop you should ensure that you're always checking a valid range. So if you want to check current item and next item your loop should only go up to the valid range:

for (int i = 0; i < states.Length - 1; i++)
{
    if (states[i] == 1 && states[i + 1] == 1)
    {
        // Do something
    }
}

The condition i < states.Length - 1 (note the -1) ensures that you never go beyond the valid range.


Edit

The above loop will only iterate from first element to the element before the last. If you want to iterate through them all, but want to check the above condition only for items before the last, then you can add an additional condition to prevent the out of range exception at the last:

for (int i = 0; i < states.Length; i++)
{
    if (i < states.Length - 1)
    {
        if (states[i] == 1 && states[i + 1] == 1)
        {
            // Do something
        }
    }
}

Edit 2

Looking at your updated post, I think your if condition needs to change to this:

if(i < states.Length - 1 && i > 0)
Sach
  • 10,091
  • 8
  • 47
  • 84