Do-It-Yourself Creating and Using Multidimensional Arrays in C++ - do it yourself

Hello again!

Many of you have used arrays before, but some of you may have wondered how you would create and use a two dimensional or even multi-dimensional array instead of having to create multiple single dimensional arrays. A few of you may have tried to create one yourself (like me) and then had the compiler complain that you were doing bizarre things that made no sense (also like me).

After doing some googling to educate myself, I will now show you how to successfully create and use multi-dimensional arrays.

Step 1: What we'll need to have

To eventually show a two-dimensional array in a visible fashion (we'll talk about the 3 dimensional array option later), we will create a recording device that uses the same materials as my single dimension array Instructable. Those materials are:

• 3 breadboard buttons (the record button, a button to have its presses recorded, and a play button)
• 3 LEDs (to indicate when each button has been pressed)
• 6 small value resistors (I will use 220 Ohms)
• Digilent's chipKIT Uno32 (the microcontroller to run the code)
• Jumper wires (not shown)

Step 2: Things to consider about multi-dimensional arrays

A two dimensional array, or any dimensional array for that matter, is in essence multiple single dimension arrays. The benefit of multi-dimensional arrays is that it can visually group related arrays together in one piece of code. That way the programmer doesn't have to call for information from multiple sources, but can specify one source and find their piece of information from there.

When you declare an single dimension array, you are essentially making a line of values. With each higher dimension, you add (believe it or not) another dimension to your line of values. A two dimensional array would make your line a plane of values, and a three dimensional array would make the plane of values a cube of values.

Going even higher you start getting into interesting perspectives. A four dimensional array could be thought of as having a line of your three dimensional cubes. Arrays of six dimensions could be thought of as a cube of cubes. And so on.

As a disclaimer though, you will rarely ever need anything beyond a three dimensional array in programming. Certainly higher dimension arrays can be used as a cataloging/database for [country] [state] [county] [town] [household] [person] or some sales purpose, but that would (in my opinion) be easier to navigate in multiple, smaller dimension arrays and you would need a ridiculous amount of memory for some of these arrays.

Step 3: Declaring and Initializing a two dimensional array

But let's get to the more practical part of actually creating these arrays. Each dimension you add to an array will add an additional [elements] in front of the first set of [elements] which specifies how long each single dimension array is.

So, for a two dimensional array, you would be declaring that you want a certain amount of single dimension arrays of a set length. For example, if I wanted to declare a 2D array that has 3 single dimensional arrays of 4 elements each I would type:

int myFirst2DArray [3] [4] ;

This nicely creates our desired array, but fills it with zero's in all 12 spots. To initialize it with certain values and not have to individually assign them later we could instead type:

int myFirst2DArray [3] [4] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
} ;

You could arrange this so it was only on one line instead, but this personally allows me to visually make sure that I have all of the values where I want them to be.

Much like single dimension arrays, we can access individual values by stating which element in each direction we want to access. For example, to get the third value of the single dimension arrays of the second array, we type:

int x = myFirst2DArray [1] [2] ;

Step 4: Declaring and Initializing higher dimension arrays

Again as you add more dimensions to a single dimension array, more [elements] are added in front of your declaration of the array. So, for a 3 dimensional array that is to have three layers of four rows of single dimensional arrays that have two elements in them each, we could initialize it by typing:

int myFirst3DArray [3] [4] [2] =
{
{ {10, 11}, {12, 13}, {14, 15}, {16, 17} },
{ {18, 19}, {20, 21}, {22, 23}, {24, 25} },
{ {26, 27}, {28, 29}, {30, 31}, {32, 33} }
} ;

Step 5: A little more on multi-dimensional arrays

Note that we could have instead spread the array out even more to help illustrate the three dimensions by:

{ // the entire array
{ // the first layer of the cube
{10, 11}, //first line/row of values in the layer
{12, 13}, etc, etc //other lines of values in the layer
}, // end of the first layer of the cube
{ // the second layer of the cube
{18, 19}, etc, etc //lines of values
} // end of the second layer
etc, etc // third layer and values
} // end of the entire array

...but that would have made the step look unnecessarily huge and gross, (and apparently Instructables is ignoring some of the spaces between the code and the comments) so the slightly more condensed version of initializing all the values in a particular layer on one line was done instead. It is through this style of nested { } that you can create multi-dimensional arrays.

Note that you do not give values to the higher order dimensions when initializing the array, just values for the single dimension arrays; otherwise you would be attempting to somehow give extra values to the array that the compiler would not know what to do with.

Step 6: The two dimensional array in practice

Let's set up our circuit so we can test the two dimensional array. If you happen to already have this circuit set up from my single dimensional array Instructable, feel free to jump ahead past the circuit construction.

Connect the negative power rail on the breadboard to one of the ground pins labeled "GND" on the chipKIT Uno32. Then run a jumper wire from the 3.3V power supply pin on the Uno32 to the positive power rail on the breadboard.

Now, place each of the three buttons slightly spaced apart on the breadboard with each button straddling the valley on the middle of the breadboard. Connect one leg of each button to the negative power rail by using one of the resistors. Connect the leg on the same side of the valley as the grounded led to the positive power rail.

Step 7: Setting up the circuit- LEDs and digital signals

For our visual portion of the circuit (which involves the LEDs, believe it or not) place an LED next to each button. Attach a 220 Ohm resistor from the cathode side of each LED to the negative power rail.

But while this looks nice, it does us no good if we can't give or receive any signals from our circuit. To remedy this, run a jumper wire from the anode side of each LED to their own digital pin on the Uno32 as well as a jumper wire from the grounded leg of each button to their own digital pin on the Uno32. I used pins 5, 7, and 9 for the buttons and pins 31, 33, and 35 for the LEDs.

Step 8: The code for the two-dimensional recording

By nature of a recording, neither we nor the microcontroller know for sure how long the recording will be until we actually stop recording. Because of this, we will have guess how big our array will have to be to store our longest recording. We won't have to worry about our size limitation for our integers since the Uno32 has a 32 bit processor, unless you were planning to hold (or not hold) the button during the recording for more than 24 days at a time.

Presuming this is the case, we, or at least I, will set the recording array size to be two sets of 50 elements since I don't plan on recording more than 25 button presses (one for the button depressed and the light off, one for the button pressed and the light on). The second set of 50 elements keeps track if the button was on or off so that way you are not just recording the length of time of the button being pressed (or not) while guessing if the button started out pressed or not at the beginning of the recording. You can download the code that I used for the Uno32 in the text file below. If you don't have it already, you will need the free MPIDE software to program the Uno32.

Feel free to check out the video for the two dimensional recording; you may notice it looks suspciously similar to the video for the single array demonstration video, but that is because the two sets of code are functionally the same since a two dimensional array is essentially a set of single dimension arrays.

Two Dimensional Array Demo.txt5 KB

Step 9: You mentioned a 3 dimensional array?

Indeed I did. But this also ties in with the inherent limitations of multi-dimensional arrays. This limitation comes in the form of memory. Most of the time when I create code, I place variables on the "stack" (a memory thing in programming) presuming they are local variables or I create global variables which are stored in a different memory type called ".data".

I am not going to attempt to explain the details/mechanics of memory in programming (mostly because I don't understand it well enough, but you can learn more here), but from what I understand when I tried to create a giant three dimensional array on the Uno32 as a type of multiplication table/cube, I ran into a stack overflow problem which means I tried to place more things in memory than I had space for, so the compiler placed the excess things in other memory locations that shouldn't be messed with, causing the program to crash when I tried to run it. I couldn't seem to convince the program that dynamically allocated memory should allow me to have more memory room either.

Basically, I ran out of memory and couldn't figure out a way to make the 3D array work.

I did eventually make a small 2D array work (60 by 60), but we just covered an example on 2D arrays, and in the end, you can just type your multiplication problem in code and get the answer without first creating an array of answers. It's the fine line between learning new things and pragmatism.

Step 10: Some final thoughts

Thank you for checking out this Instructable! To see what else the Digilent team has been up to, check out the Digilent MakerSpace and the Digilent Blog!

If you have any questions or comments about this Instructable, please feel free to message me or put them in the comments below and I will address them as soon as I am able to the best of my ability.

•