In this video, we'll look at the syntax for multi-dimensional arrays,
in c++.
We'll focus on two-dimensional arrays,
but you can use as many dimensions as they're supported by your system.
While there's no real limit to the number of dimensions you can have for arrays,
be aware that some compilers do place limits on this.
Though the limits are quite high and you're unlikely to reach them.
The syntax for declaring multi-dimensional arrays just involves adding another set of
square brackets with the new dimension inside them.
In this example, you can see we're declaring a two-dimensional array named movie rating.
This array has three rows and four columns,
which means that the total size of the array is 12 integers.
We have not initialized this array,
so we should assume that the array elements contain garbage data until initialized.
Let's see how we might model real-world data using this 2D array.
Suppose we want to collect movie ratings.
We have movie reviewers, and each reviews a series of movies
with an integer rating from 0 to 5.
We have three reviewers and each has to review the same four movies.
We could choose to model this data by having one array of movie ratings for each reviewer.
So we could declare three arrays of four integers each and solve our problem like that.
Another option is to use a two-dimensional array.
The first dimension or row in our grid is the reviewer.
And there would be four columns in the grid, one for each movie.
This is similar to the layout of a spreadsheet.
In the figure, the reviewer is the first index or row of our array labeled in red.
And the movie is the second index or column of our array labeled in green.
Now it's pretty easy to go to a specific row and column
and get or set that movie rating for that specific reviewer and that specific movie.
Accessing the specific movie rating is easy.
I can ask the user to enter a movie rating as an integer and store it in movie ratings sub-1, sub-2.
Suppose the user interest is 5.
This would update that specific element in the array.
I can also display that element to the console using the same syntax.
Initializing multi-dimensional arrays
uses the same initializer list syntax that we saw with one-dimensional arrays.
In this case, we provide in the initializer list
the rows of columns.
You could do this in a single line, but it's much more readable if you split
it across lines as I've done here.
You can see the initializer is providing three rows of four integers each
which is exactly how we declared the array.
While one and two dimensional arrays are by far the most commonly used arrays in programming,
extending the syntax to three dimensions and more is just as you would expect.
Don't forget that the same characteristics hold for multi-dimensional arrays
as for single-dimensional arrays.
We'll see multi-dimensional arrays again when we talk about iteration in c++.
So that's a look at c++ arrays.
If you see them in existing code, you'll understand how they work,
what they look like and their syntax.
Remember, at the beginning of the section, I mentioned that we rarely use c++
arrays in moderate c++. Instead, we use vectors.
Let's take a quick look at c++ vectors.