C++ Tutorial # 18: Multidimensional Arrays

Today I’m going to teach you something that will blow your mind! In this tutorial, we will be using for loops again, and create a multidimensional array to create an awesome program. If you haven’t read my tutorial on arrays or don’t remember it well enough, read it again. It’s important you know the basics of arrays before you try to work on multidimensional arrays. Before we begin, let me answer this question for you:

What is a multidimensional array?

Think of it like this; it’s an array of arrays. Simple right? Yes, but not when you put it like that! Think about what I taught in lesson #8, this is the syntax for an array:

type variable [elements]

We know that you can create an array by initializing a variable first, and then place the number of elements you want in your array inside of the square brackets [ ]. These square brackets are called dimensions. To add another dimension, you just add another pair of square brackets after the first pair and place the number of elements you want inside of it.

Here’s an example:

int 2dArray[2][3]

This is an example of a two-dimensional array meaning that it has two dimensions. So if I were to add another dimension, it would make it a three-dimensional array. You can have as many arrays as you’d like, but keep in mind that having more arrays use up more memory.

So what do these arrays look like?

The image below is an example of a one-dimensional array. Remember, the order of the elements starts with zero (not one) and goes up.

This image is an example of a two-dimensional array. The order of the elements also start with zero and up, but the array goes in two directions. Think of it as a table or a flat graph with the x & y-axis.

The last one I’ll show is, of course, a three-dimensional array. The array starts to look like a 3D cube.

While there are fourth & fifth-dimensions, I can’t draw them obviously, it would be too weird.

Before we begin programming, let me tell you this; in C++ you can have as many dimensions you want, keep in mind though that it takes up a ton of memory.

Now that you get the picture of how multidimensional arrays work, let’s get started on making a program using them.

Begin coding!

Copy, paste, or write this example:

If you compile and run, the program should look like this:

7 1 0 0

0 2 0 0
0 0 6 0
0 0 0 4

Let me explain how this works:

In this section of our code, we create an integer variable called table, gave it two dimensions with four elements in each dimension. We create integer to use later in our for loop.

Then we create a double for loop. This loop assigns every element in the two-dimensional array table to the value of zero.

We can’t see our array, but it should look something like that:

Notice how this is not the same as the regular directions of the x & y-axis.

Using what we learned in lesson #8, we assign values to specific elements in our multidimensional array.

Then we create another double for loop, but instead of assigning the values of each element to zero, we print out the value of each element separated by a space. Take a look at the if statement, when the condition y == 3 becomes true, our program prints a new line. Kind of cool right? So when we have a double for loop, the first for loop executes the second for loop. After the second loop completes, the first loop executes again until it’s condition becomes false.

So why use multidimensional arrays? In lesson #8, I told you that you can use them to program your very own tile map editor. You still need a lot to learn to do something like that, but let me give you an idea of how it works. You can use an API such as SFML and program something to read and write a text file. This text file can be read by the program and take sections or tiles from a .png file and render it in several coordinates on the screen. These coordinates you use are multidimensional arrays or vectors. If you also read my first pixel art tutorial, you’ll gain a better understanding of how this works.

I hope you have enjoyed this tutorial. Don’t forget  to subscribe to my newsletter if you’re interested, and please share and comment. I’ll be back with more tutorials soon.