[SOLVED] First row of array returning random values

I'm declaring a 2D array of integers and incrementing a counter from 0-7. Then I'm trying to read back the corresponding elements of each row of the array separately. I'm getting the correct values back from the second row, but random values from the first.

Here's the code:

const int ditArray[2][8] = {
  {300, 300, 300, 300, 300, 300, 300, 1000},
  {1, 0, 2, 0, 3, 0, 4, 0}
};
//Initialise other array-related stuff
int currentElement = 0;

void setup() {
  //Initialise comms to the serial monitor
  Serial.begin(9600);
}


void loop() {

  //Get elements from array
  Serial.print("From [currentElement][0]: ");
  Serial.println(ditArray[currentElement][0]);
  Serial.print("From [1][currentElement]: ");
  Serial.println(ditArray[1][currentElement]);
  Serial.print("currentElement: ");
  Serial.println(currentElement);
  Serial.println();

  //Increment the array
  if (currentElement >= 7) {
    currentElement = 0;
  }
  else {
    currentElement = currentElement + 1;
  }

  //Stability delay
  delay(1000);
}

This is returning the following:

From [currentElement][0]: 300
From [1][currentElement]: 1
currentElement: 0

From [currentElement][0]: 1
From [1][currentElement]: 0
currentElement: 1

From [currentElement][0]: 2573
From [1][currentElement]: 2
currentElement: 2

From [currentElement][0]: 27717
From [1][currentElement]: 0
currentElement: 3

From [currentElement][0]: 28015
From [1][currentElement]: 3
currentElement: 4

From [currentElement][0]: 28005
From [1][currentElement]: 0
currentElement: 5

From [currentElement][0]: 27717
From [1][currentElement]: 4
currentElement: 6

From [currentElement][0]: 15616
From [1][currentElement]: 0
currentElement: 7

As you can see, ditArray[1][currentElement] works but ditArray[currentElement][0] doesn't.

I'm really confused. I've been trawling the forums for hours now but I just haven't been able to find anyone else with the same problem. Any insight would be greatly appreciated, I'm fairly new to programming in general so I'm sure it's a beginners mistake that I just can't see!

As you can see, ditArray[1][currentElement] works but ditArray[currentElement][0] doesn't.

Is that what you see?

TheMemberFormerlyKnownAsAWOL:
Is that what you see?

I'm not sure what you're asking me.

The data above (returned from the serial monitor) gives me all the correct values from the second row, but garbage for all by the first value from the first row.

Look at the definition of your array. The first set of square brackets encloses a 2, which means that it can take zero or one. Your code uses numbers from zero to seven there. Small wonder that you're getting funky results.

const int ditArray[2][8]

That is an array of 2 arrays. Each array has 8 elements. The arrays are numbered 0 and 1.

From [currentElement][0]: 2573
From [1][currentElement]: 2
currentElement: 2

Here (top line) you ask for the 0th element of the 3rd array in an array that contains 2 arrays. The 3rd array does not exist so you get whatever happens to be in the memory at that location.

That's fantastic, thank you! I should have

[0][currentElement] and [1][currentElement].

Very much a beginner's mistake, I'm back on track! I think in my head I was thinking "swap from one row to the other", which has caused my to swap them round in my code.