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:
Values can then be modified and accessed in the array, much like in 1D arrays, by using the square brackets. So
Table 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:
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
With a simple, clever
cout addition - this data could be outputted to the user:
1 2 3 4 5 6 7 8 9 10
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