When I searched around, looking to learn how to do a REAL multi-dimensional array, because I needed one as a constant, I can’t seem to find anywhere, where anyone tackles anything beyond two elements. So I just kind of figured it out, and I felt that I needed to write this up so that someone somewhere down the line, who finds themselves in this position can see by clear example, how to dimension AND populate an array of ANY size elements with relative ease.

Starting with the simple binary two dimensional array - and I’m using INT here because it’s easy to see plainly, but it doesn’t matter what the datatype is, the array arrangement will be the same, it’s only the information inside the array that will be different, like for Strings, you would have double quotes and characters inside those quotes between the comma’s where I simply have x,y,z because I’m trying to keep it simple and obvious.

```
myArray[3][3] = {
{a,b,c},
{d,e,f},
{g,h,i},
};
```

Obvious, right? Three rows of three and you would address each row like this

```
:[0][0],[0][1],[0][2],
[1][0],[1][1],[1][2],
[2][0],[2][1],[2][2],
[center]ex: myArray[1][1]=e; myArray[2][0]=g;[/center]
Make sense? But what if I needed 4 rows of 5?
[tt] myArray[4][5] = {
{a,b,c,d,e},
{f,g,h,i,j}, ex: myArray[2][3]=n; myArray[3][2]=r;
{k,l,m,n,o},
{p,q,r,s,t},
};
```

Easy, right? But what happens if we go even further with this? Let’s try a 3 element array:

```
myArray[5][2][3] = {
{{x,y,z}, {x,y,z}},
{{x,y,z}, {x,y,z}},
{{x,y,z}, {x,y,z}}, ex: myArray[2][2][0]=x (third row, left x)
{{x,y,z}, {x,y,z}},
{{x,y,z}, {x,y,z}},
};
```

Can you see how the brackets define each element? Let me see if I can make it clearer:

{ ← Outside bracket always contains the element on the left [5] in this case

{ ← Next element

{ ← final element

Let me show you that again, but with the brackets colored to match the array declaration:

```
myArray[color=red][5][/color][color=green][2][/color][color=blue][3][/color] = [color=red]{[/color]
[color=green]{[/color][color=blue]{[/color]a,b,c[color=blue]}[/color], [color=blue]{[/color]d,e,f[color=blue]}[/color][color=green]}[/color],
[color=green]{[/color][color=blue]{[/color]g,h,i[color=blue]}[/color], [color=blue]{[/color]j,k,l[color=blue]}[/color][color=green]}[/color],
[color=green]{[/color][color=blue]{[/color]m,n,o[color=blue]}[/color], [color=blue]{[/color]p,q,r[color=blue]}[/color][color=green]}[/color], ex: myArray[2][1][0]= j;
[color=green]{[/color][color=blue]{[/color]s,t,u[color=blue]}[/color], [color=blue]{[/color]v,w,x[color=blue]}[/color][color=green]}[/color], myArray[4][0][3]= aa;
[color=green]{[/color][color=blue]{[/color]y,z,aa[color=blue]}[/color], [color=blue]{[/color]bb,cc,dd[color=blue]}[/color][color=green]}[/color],
[color=red]}[/color];
```

Can you see how the groupings match the elements? 5 rows bracketed in RED.

Within each of those 5 rows, we have TWO sets of brackets separated by a comma.

Then within each of those two elements, we have our final three numbers

Now let’s go into something even deeper - this is a real structure that I use to define motor speeds for a given length of time. A method cycles through the different sets of data and uses the first two integers as motor values than the third integer as a delay time … to make some motors do specific things.

const int motorSettings[6][2][5][3] =

{

{{{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}, {{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}},

{{{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}, {{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}},

{{{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}, {{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}},

{{{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}, {{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}},

{{{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}, {{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}},

{{{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}, {{x,y,z},{x,y,z},{x,y,z},{x,y,z},{x,y,z}}},

};

Can you see it now? It should be easy to define and populate pretty much any array that you need. It’s not some enigmatic mystery that requires multi-dimensional spacial thought like so many people say…

I hope this helps someone in the future.

Thank you,

Mike Sims