Hello again!
In this video, we are going to look at two-dimensional arrays.
We are going to use a two-dimensional array for the Game of Life.
So I want to make sure that we are fully up to speed, before we get into the implementation.
Let's imagine that we have some data, which is in tabular form.
So this could be a table, in a word-processing document, or some cells in a spreadsheet.
We have two rows and four columns.
In C++ terms, we can represent this by a two-dimensional array.
So this is an array, whose elements are themselves arrays.
The outer array will represent the rows of this table, and the inner arrays will represent the columns
of this table.
So we have an array which has two elements.
Each element is itself an array of four standard strings.
In memory, a two-dimensional array is stored as a single memory block.
So we have the first row followed by the second row.
So it actually looks very much like a one-dimensional array
We can in fact initialize it as though it were a one-dimensional array, so we can just write all the
elements, one after the other.
But it is better to initialize as nested arrays.
So we have the first row and then the second row.
We can use subscripting with two-dimensional arrays. If we just have a single subscript, that would
apply to the outer array.
So that will give us one of the rows.
So if we put "names" subscript one, this will give us an array with Barney and his family as the elements.
If we have a second subscript, then that will give us the element within the inner array.
So we would get the element with index two, which is "Bamm-Bamm".
If we're iterating over a two-dimensional array, we start off with the first row, then we go through
all the columns in that row.
Then we go on to the next row, we go through all the columns and so on, until we have gone through all
the rows.
So we iterate over all the rows, and then for each row we iterate over all the columns, and then
we can get the element in that row, at that column.
So let's try this out.
Here is our two-dimensional array.
We are going to print out the element in row one, column two, and then we are going to print out all the
elements.
I have put a new line after each row iteration, so all the elements for each row should be on a separate
line.
And there we are, the element in row one, column two is "Bamm-Bamm", and there we have the elements, with
each row on a separate line.
So a two-dimensional array looks very much like a one-dimensional array, as far as the program is concerned.
And sometimes it is actually useful to store all this data in a one-dimensional array. So we could do this.
So we have our one-dimensional array with all the characters in it.
This will have eight elements, because there is four rows and two columns, and four times two is eight.
Then this will work just like any other one-dimensional array.
So if we subscript, we get the element.
So subscript one is "Wilma". If we want to get the element at some row and some column,
We need to calculate how far into the array we need to go, to get to that element.
If we multiply the row by the total number of columns, and then add the column that will give us the
right offset.
So if we wants the element in row with index one and column index two, then one times four.
So we go past the first row. That is four elements. And then another two.
So "Bamm-Bamm" is the element we want.
And what is the point of this?
Why do we want to make life more complicated?
The thing is, if you have a two-dimensional array, and you access an element in one of the inner arrays,
then that requires a pointer dereference.
Whereas, if you do it this way, you replace the pointer dereference by a multiplication, and on modern
hardware, multiplying is much faster than dereferencing a pointer.
So if you have an application which is performance critical, like games programming or scientific computing,
for example, then this can be a useful trick.
On the other hand, if you want nice, simple notation, then you can use a two-dimensional array instead.
So let's quickly try this out.
Here is our flattened-out, two-dimensional array. It is now a one-dimensional array.
We are going to get the element in row one, column two, and then we are going to print out all the elements
again.
So, instead of having the extra pointer dereference, we calculate the position of the element within
the array.
And there we are.
So we get the same results again.
Okay.
So that is it for this video.
I will see you next time.
Until then, keep coding!