Multidimensional arrays

Today I was learning more about arrays and found out that there are not any good descriptions of multidimensional arrays.

I created this sketch to demonstrate a 3D array. Maybe it can be added some place to help others.

/*
    Multi-Dimension Array Demo
    
    Demonstrate the usage of a multi-dimension array
      and how to assign initial values.
     
*/

void setup(void) {
  /* initialize inputs/outputs
     start serial port
     Used for serial monitor  */
  Serial.begin(9600);
}

void loop(void) {
  //  initialize variables

  int a = 0;
  int b = 0;
  int c = 0;

  /*  Initilize the array
        Put values into each location
        my3dArray[c][b][a]
        The values are nested from the left to right with {}
        braces to hold the values.
        On the right comments are the array locations being loaded.
   */

  int my3dArray[4][3][2] = {
    {              //  array[0]...
      { 1, 2 },    //  array[0][0]... 
      { 3, 4 },    //  array[0][1]...
      { 5, 6 }     //  array[0][2]...
    },
  
    {              //  array[1]...
      { 11, 12 },  //  array[1][0]...
      { 13, 14 },  //  array[1][1]...
      { 15, 16 }   //  array[1][2]...
    },
  
    {              //  array[2]...
      { 21, 22 },  //  array[2][0]...
      { 23, 24 },  //  array[2][1]...
      { 25, 26 }   //  array[2][2]...
    }, 

    {              //  array[3]...
      { 31, 32 },  //  array[3][0]...
      { 33, 34 },  //  array[3][1]...
      { 35, 36 }   //  array[3][2]...
    } 

  };

  //  End loading values into the array

  //  Loop to display the array on the Serial Monitor

  for( c = 0; c < 4; c++) {     //  Initialize c loop 0-3
    for( b = 0; b < 3; b++) {   //  Initialize b loop 0-2
      for( a = 0; a < 2; a++) { //  Initialize a loop 0-1 
        Serial.print(c);        //  Print c variable value
        Serial.print(", ");
        Serial.print(b);        //  Print b variable value
        Serial.print(", ");
        Serial.print(a);        //  Print a variable value
        Serial.print(", ");
        Serial.println( my3dArray[c][b][a] );  //  Print out the array value
       };
    };
  };
}

//  End program

_3D_array.ino (1.9 KB)

Well there is also this tutorial: Arrays - C++ Tutorials :stuck_out_tongue:

The issue I was having was defining all the data at the beginning. I spent a fair bit of time searching and couldn't get the correct sequence down. I could only find an example for a 2D array code example.

That tutorial is like most of them. Covers just one dimension in detail and then doesn't go into any details for defining the data when you add more dimensions. It shows loops for putting data into the array, but that was no issue. That worked the first time I tried it.

I spent time looking at the array backwards for defining and I wanted to help others. I tried many things until I found what I was doing wrong. I wanted to have the array filled with data before the main part of the program was running.