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)