Team 2550
Technical Documentation
 All Files Variables Pages
Multidimensional Arrays

Multidimensional arrays allow you to create arrays with more than one index. This can be useful for creating tables or simulating real life within your program.

Memory Allocation Diagrams

Memory allocation refers to how and where variables and other temporary values are stored while the program is running. There are two types of memory (RAM) storage: the stack and the heap. For now, we will just focus on the stack - the portion of memory that the system dedicates to your program.

Given this code...

char c;
int x;
int y[5];

Here is a memory chart...

Start Address End Address Unit Size Name
10000 10001 1 byte c
10001 10005 4 bytes x
10005 10024 4 bytes y
Note
An int takes 4 bytes of memory. 1 byte is 8 bits, a bit is a single 1 or 0. In most (if not all) cases, memory is addressed by the byte. You can only read and write 8 bytes at once.

The above table is fairly similar to the one that the system keeps, but not exactly the same. The array takes 5x the memory of the int. Because that is not immediately visible, memory is often shown in diagrams or charts. For instance, a chart of the above memory could look like this...

Address Name Type
10000 c char
10001 x int
10002
10003
10004
10005 y[0] int[5]
10006
10007
10008
10009 y[1]
10010
10011
10012
10013 y[2]
10014
10015
10016
10017 y[3]
10018
10019
10020
10021 y[4]
10022
10023
10024
...
Note
The addresses do not matter, I am just giving each byte an address. This is a simplification of reality, but it works for illustrative purposes.

The benefit of the above chart is that it shows how much space is taken up by each variable.

Arrays of Arrays

Multidimensional arrays allow you to store arrays of arrays. For instance, a declaration of a 2 dimensional array would look like this...

int x[3][2];

The array above has two indexes, and is known as a 3x2 array. You can store 6 values in it. To access a value, you treat it like a normal array.

x[0][0] = 0;
x[2][1] = 3;

One way to think of a 2D array is as a table. It would look something like the one below...

x[3][2]
x[0][0] x[0][1]
x[1][0] x[1][1]
x[2][0] x[2][1]

It is important to note that the table is arranged so that the first index defines the number of rows and the second columns. This is different from what you commonly learn in math class, where you use X and Y coordinates (columns/rows instead of rows/columns). The reason for using the rows/columns (RC) convention is that, to the compiler, you are defining an array of arrays. In other words, memory is not 2D.

x[3][2] in Memory
Address Name
1000 x[0][0]
1001
1002
1003
1004 x[0][1]
1005
1006
1007
1008 x[1][0]
1009
1010

1011

1012 x[1][1]
1013

1014

1015

1016 x[2][0]
1017

1018

1019
1020 x[2][1]
1021
1022
1023

The same concept applies to arrays of 3 or more dimensions. I won't make a table of of a 3D array, they have a tendency to become very large...

int a[4][5][6]; //A 4x5x6 3-dimensional array (120 places)
float b[5][6][7][8]; //A 5x6x7x8 array (1680 places)
Note
In general, it is best to declare the fastest changing (largest) index last.

More information is available in this Wikipedia article.

Initializing

Initializing the values of a multidimensional array is slightly more complicated than single-dimensional ones.

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

Each row is enclosed in its own pair of braces, and the whole statement is also enclosed. Again, note that the x coordinates (columns) are the second dimension.

Note
Putting a single value such as {1} does not work for multidimensional arrays, the easiest way to initialize the values is via a loop.

You can also let the compiler define the first dimension. The code below is valid.

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

However, this does not work.

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

You have to define every dimension except for the first.

Warning
Declaring an array greater than 2 dimensions in this manner is not advised. It will get confusing.
Note
You can only use these initializations for newly declared arrays. After declaration, you have to assign values individually by their index.

Looping

You can access all of the elements of a multidimensional array by nesting for-loops. Again, the dimensions of the array are not stored automatically.

This code initializes the values of a 2D array...

#include <iostream>
using namespace std;
int main() {
int a[5][10]; //[y][x]
int count = 1;
for (int y = 0; y < 5; ++y)
{
for (int x = 0; x < 10; ++x)
{
a[y][x] = count;
count++;
cout << a[y][x] << ' ';
}
cout << '\n';
}
}

Output

1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 

It is important that you are consistent with the dimensions, and it helps to make a comment saying what each index is. That way, you know that the loops don't get mixed up. In the case above, the X value had to be the second coordinate because the there is a newline for every change in Y. Once you have printed a newline, you can't go back up.

Here is another example, this time using a 3D array...

#include <iostream>
using namespace std;
int main() {
int a[5][5][5]; //[z][y][x]
for (int z = 0; z < 5; ++z)
{
for (int y = 0; y < 5; ++y)
{
for (int x = 0; x < 5; ++x)
{
a[z][y][x] = x + y + z;
cout << a[z][y][x] << ' ';
}
cout << '\n';
}
cout << "\n\n";
}
}

Output

0 1 2 3 4 
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 


1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 
5 6 7 8 9 


2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 
5 6 7 8 9 
6 7 8 9 10 


3 4 5 6 7 
4 5 6 7 8 
5 6 7 8 9 
6 7 8 9 10 
7 8 9 10 11 


4 5 6 7 8 
5 6 7 8 9 
6 7 8 9 10 
7 8 9 10 11 
8 9 10 11 12 

Here, I have output the array values so that each layer (z index change) is printed separately, and the x and y indexes are shown in a table. As you add more dimensions, it becomes increasingly difficult to display their data all at once. For most programs, you will never need more than 3 dimensions.

Functions

Like single-dimensional arrays, multidimensional arrays are always passed by reference. Also, all dimensions except for the first must be defined at compile time.

#include <iostream>
using namespace std;
void userInput(int a[][3], int y_size);
int main() {
int a[2][3];
userInput(a, 2);
cout << '\n';
for (int y = 0; y < 2; ++y)
{
for (int x = 0; x < 3; ++x)
cout << a[y][x] << ' ';
cout << '\n';
}
}
void userInput(int a[][3], int y_size) {
for (int y = 0; y < y_size; ++y)
{
cout << "> ";
for (int x = 0; x < 3; ++x)
cin >> a[y][x];
}
}

Input/Output

> 1 2 3 
> 4 5 6

1 2 3 
4 5 6
Note
If you want to guarantee that the function will not change the array, you can put const in front of the parameter (const a[][3]).