In C/C++ you even can create arrays of arrays. These are called multidimensional arrays and they can be of any dimensions you want, but the most used is 2D.

The term dimension indicates the number of indices used to access an element of an array.

In one of the previous posts, Learn C programming: Arrays and Everything you Need to Know, you learnt about mono-dimensional arrays, which require only one index.

The dimension of an array is specified during its definition and it can be visually represented through geometric shapes.

To understand better, have a look at this image:

Visualisation of arrays

Obviously, a computer doesn’t memorise a multidimensional array as a shape, but as a sequence of values, just like normal arrays.

multidimensional arrays in the memory

→ On Beginnersbook there’s a great image that explain how a 2D array is stored in the computer memory.

There aren’t standard limits on the maximum dimension of an array, but it varies for each compiler.

However a compiler must be able to manage at least 6 dimensions.

Initialising a multidimensional array


The syntax of a multidimensional array is:

Where:

  • type is the type of the array and so of all its elements.
  • name is the name of the array.
  • [length1]…[lengthN] are the lengths of all the dimensions of the array.

For each dimension of an array you have to specify its length inside square brackets.

For example:

  • 1D
  • 2D
  • 3D

If you leave some brackets without content, the compile will fill them automatically for you, but only if you initialised the array.

Note the difference between the words length and size:

  • the length is the number of elements inside a single dimension and you find it between square brackets.
  • the size is the total number of elements inside the array and it’s equal to the product of the multiplication of all the lengths.

In order to initialise an array, you have to use curly brackets, just like you do for mono-dimensional arrays.

Except that for multidimensional arrays you have to use nested brackets ( brackets inside other brackets ) to initialise each dimension.

A lot of people forget the right order of initialisation, because it’s quite confusing.

Therefore I’ve prepared an easy trick to help you remember it.

First of all, let’s see the final result for a 3D array:

Trick

This is the step-by-step process you can use to remember the right order of the brackets:

1. First, define the array and write the main pair of brackets:

2. Then look at the first length from left to right ( [2] ) and write as many pairs of curly brackets as indicated by that number:

Note: between each element of a dimension you must put a comma, but not after the last one

3. Now, repeat the step #2 for the second length ( [4] ) and for each pair of brackets you wrote previously:

4. If you have a different number of dimensions than the one in the example, repeat this step for each of them, except for the last one.

5. For the last dimension, insert inside each pair of brackets from the step #3 a number of values equals to its length ( [3] ):

Done!

With this method you’ll never end up switching rows and columns for error!

Now, let’s turn this table into an array:

0 1 2 3
0 ‘C’ ‘g’ ‘q’ ‘i’
1 ‘a’ ‘h’ ‘r’ ‘b’

First of all, you must extract all the proprieties of the array you have to create:

  • The array will be of type char, because all the values inside the table are characters.
  • There are 4 columns and 2 rows, so the lengths of the multidimensional array will be 2 and 4

Here’s the array:

Why is it first rows and then columns? Because when you add a new dimension, you have to specify its length before the other ones.

If to a mono-dimensional array

you add another dimension – more rows in this case – you have to put the new pair of square brackets before the other one:

Access an element from a multidimensional array


In order to access an element from a multidimensional array, you have to specify as many indices as dimensions.

Consider the following 2D array:

The first element is in the first row and first column, so to access it, write

To get the third element of the second row, write this:

access an array element

Reading and writing


Once you know how to access an element of an array, you can easily set and write its value.

Therefore this section will focus on how read and print a whole array.

To do so we need a modified version of the traverse algorithm we saw in one of the previous articles.

It consists of a number of for loops, one inside the other, equals to the number of dimensions.

Let’s read a 2D array of lengths 5 and 3.

You can use the same code to print all the values in a multidimensional array.

Just replace scanf with printf

Remember to follow the same order of initialisation.

If you created an array that represent a rectangle of height 6 and length 5 ( [6][5] ), the first for and index would indicate its height, while the second ones its length.

This example prints a house 😛 :

Let’s analyse today’s program


At the top of this post you can find today’s program.

Its purpose is printing a magic cube on the command prompt.

A magic cube is a grid of numbers in which the sum of each column, row and diagonal is always the same.

I found the values on the Internet.

int magic_square[5][5] =

It creates a 5×5 array of integers and it initialise it.

Each pair of curly brackets represent a row, while columns are represented by elements.

for(int k=0; k

This loop goes through each row of the array ( its first dimension ).

for(int j=0; j

This loop, instead, goes through each column of the current row ( 2nd dimension ).

printf("%d\t", magic_square[k][j]);

This printf prints the value of the element found at the indices combination k and j and it moves the cursor to the next tab space to make the result look like a table.

printf("\n");

At the end of each row, the second for ends, so the program will go to a new line and then it will enter that for again.

Translation of today's program in human language


Computer, include the standard C library for inputs and outputs, stdio.h

Here's the main body of the program

Define a multidimensional array of type int and both lengths 5 and initialise it to the following values

1st row: 17, 24, 1, 18, 15

2nd row: 23, 5, 7, 14, 16

3rd row: 4, 6, 13, 20, 22

4th row: 10, 12, 19, 21, 3

5th row: 11, 18, 25, 2, 9

Print "What is a magic square?" and then go to the next line

Now print "A magic square is a square grid in which the sum of each row and column is constant." and go to a new line again

Execute the following code 5 times:

Execute the following code 5 times:

Print the current value of magic_square and go to the next tab space in order to form a table

Go to a new line

Conclusion


Today you've learnt what multidimensional arrays are and how to use them.

You also learnt a trick to easily remember how to initialise them without making confusion, even for arrays with lots of dimensions.

If you have any doubts, feel free to ask, writing a comment below.

Otherwise, if you've appreciated it and you think it might help a friend of yours, share it.

Anyway, the post is finished, have a good day and we will see next week!

From Zephyro it’s all, Bye!


Leave a Reply

%d bloggers like this: