C++: Multidimensional Arrays

Looking for in-depth C tutorials? Try SourceCrunch.

So we've talked about arrays before, however if we delve a little deeper, we can actually have arrays which have multiple dimensions! If you think of one array as a line of pieces of data, you could have an array of array which would essentially be a line of lines - so visually, a square of data. To take this a step further, you could have an array of arrays of arrays which would essentially give each point another line, thus visually creating the third dimension, making a cube of data. We could of course go on to the fourth, fifth, and sixth dimensions (and beyond!), however these can become a little difficult to visualize -- for reference, visualization as a line of cubes, square of cubes, and cube of cubes respectively will usually get you some reasonable comprehension of arrays up to 6D.

Take a moment to just digest that dimension theory if you don't already understand it, and then think about how this could be useful. If you think about a simulation of a 2D world for example, you could store data for each point or block in the world in what is essentially a table cell - a piece of data in a 2D array. Similarly, some sort of "real world" 3D simulation could store data for blocks, or chunks, or even particles in a 3D array (which would essentially have an 'x', 'y', and 'z' point if you are familiar with 3D co-ordinates).

Ok, we're done with theory for now - let's talk about to actually create multidimensional arrays. As I touched on earlier, a 2D array is actually just an array of arrays, and a 3D array is actually just an array of arrays of arrays. As such, instead of using one set of square brackets to create a basic 1D array, we could use two sets to create a basic 2D array, three sets to create a 3D array, and so on. A simple 2D array which essentially creates a 10×10 square "table" which stores integers as each "point" or "cell" could be created as follows:

1
int Table[10][10];

Values can then be modified and accessed in the array, much like in 1D arrays, by using the square brackets. So Table[0][1] would refer to the second ('1'st) element inside first ('0'th) element (remember that computers count from '0'). We can also initialize the values of an array by using curly brackets much like in single-dimensional arrays, however we must nest them to create the "array in array" effect necessary for different dimensional arrays. In this example we might want something like the following:

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

Multidimensional arrays can also be easily initialized using loops, specifically (and most commonly), nested 'for' loops. Take, for example, the following code snippet in which 'i' loops through the rows and 'y' loops through the columns:

1
2
3
4
5
6
7
8
9
int Table[10][10];

for(int i = 0; i < 10; i++)
{
	for(int y = 0; y < 10; y++)
	{
		Table[i][y] = 1; //initialize all "cells" to 1
	}
}

With a simple, clever cout addition - this data could be outputted to the user:

1
2
3
4
5
6
7
8
9
10
int Table[10][10];

for(int i = 0; i < 10; i++)
{
	for(int y = 0; y < 10; y++)
	{
		Table[i][y] = 1;
		cout << "Row "<< i+1 << ", Column "<< y+1 << ": "<< Table[i][y] << endl;
	}
}

With some further modifications to this, we could actually output the data in a table-like format. All we have to do is space-separate each column, and put a new line between rows:

1
2
3
4
5
6
7
8
9
10
11
int Table[10][10];

for(int i = 0; i < 10; i++)
{
	for(int y = 0; y < 10; y++)
	{
		Table[i][y] = 1;
		cout << Table[i][y] << " ";
	}
	cout << endl;
}