WEBVTT

00:00.060 --> 00:00.570
Hello again!

00:00.870 --> 00:03.690
In this video, we are going to look at two-dimensional arrays.

00:04.200 --> 00:07.050
We are going to use a two-dimensional array for the Game of Life.

00:07.380 --> 00:11.160
So I want to make sure that we are fully up to speed, before we get into the implementation.

00:12.850 --> 00:15.740
Let's imagine that we have some data, which is in tabular form.

00:16.120 --> 00:21.820
So this could be a table, in a word-processing document, or some cells in a spreadsheet.

00:22.420 --> 00:25.870
We have two rows and four columns.

00:26.890 --> 00:30.580
In C++ terms, we can represent this by a two-dimensional array.

00:31.030 --> 00:34.570
So this is an array, whose elements are themselves arrays.

00:35.900 --> 00:43.820
The outer array will represent the rows of this table, and the inner arrays will represent the columns

00:43.820 --> 00:44.480
of this table.

00:45.110 --> 00:47.870
So we have an array which has two elements.

00:48.200 --> 00:51.590
Each element is itself an array of four standard strings.

00:53.530 --> 00:58.400
In memory, a two-dimensional array is stored as a single memory block.

00:58.420 --> 01:02.260
So we have the first row followed by the second row.

01:02.680 --> 01:05.650
So it actually looks very much like a one-dimensional array

01:07.750 --> 01:12.490
We can in fact initialize it as though it were a one-dimensional array, so we can just write all the

01:12.790 --> 01:13.960
elements, one after the other.

01:14.560 --> 01:17.950
But it is better to initialize as nested arrays.

01:18.370 --> 01:21.250
So we have the first row and then the second row.

01:24.550 --> 01:30.490
We can use subscripting with two-dimensional arrays. If we just have a single subscript, that would

01:30.490 --> 01:31.930
apply to the outer array.

01:32.410 --> 01:34.030
So that will give us one of the rows.

01:34.600 --> 01:41.740
So if we put "names" subscript one, this will give us an array with Barney and his family as the elements.

01:43.060 --> 01:48.020
If we have a second subscript, then that will give us the element within the inner array.

01:48.550 --> 01:52.450
So we would get the element with index two, which is "Bamm-Bamm".

01:55.380 --> 02:01.230
If we're iterating over a two-dimensional array, we start off with the first row, then we go through

02:01.230 --> 02:02.400
all the columns in that row.

02:02.850 --> 02:07.180
Then we go on to the next row, we go through all the columns and so on, until we have gone through all

02:07.180 --> 02:07.590
the rows.

02:08.160 --> 02:14.970
So we iterate over all the rows, and then for each row we iterate over all the columns, and then

02:14.970 --> 02:18.420
we can get the element in that row, at that column.

02:21.590 --> 02:22.670
So let's try this out.

02:23.000 --> 02:25.010
Here is our two-dimensional array.

02:25.640 --> 02:31.460
We are going to print out the element in row one, column two, and then we are going to print out all the

02:31.460 --> 02:31.880
elements.

02:32.300 --> 02:37.760
I have put a new line after each row iteration, so all the elements for each row should be on a separate

02:37.760 --> 02:38.090
line.

02:40.040 --> 02:47.120
And there we are, the element in row one, column two is "Bamm-Bamm", and there we have the elements, with

02:47.120 --> 02:48.410
each row on a separate line.

02:50.000 --> 02:55.070
So a two-dimensional array looks very much like a one-dimensional array, as far as the program is concerned.

02:55.820 --> 03:02.140
And sometimes it is actually useful to store all this data in a one-dimensional array. So we could do this.

03:02.150 --> 03:06.050
So we have our one-dimensional array with all the characters in it.

03:06.500 --> 03:12.230
This will have eight elements, because there is four rows and two columns, and four times two is eight.

03:13.400 --> 03:16.040
Then this will work just like any other one-dimensional array.

03:16.040 --> 03:18.610
So if we subscript, we get the element.

03:18.620 --> 03:24.830
So subscript one is "Wilma". If we want to get the element at some row and some column,

03:25.250 --> 03:29.960
We need to calculate how far into the array we need to go, to get to that element.

03:31.790 --> 03:37.580
If we multiply the row by the total number of columns, and then add the column that will give us the

03:37.580 --> 03:38.350
right offset.

03:38.870 --> 03:45.530
So if we wants the element in row with index one and column index two, then one times four.

03:46.070 --> 03:50.000
So we go past the first row. That is four elements. And then another two.

03:50.510 --> 03:53.120
So "Bamm-Bamm" is the element we want.

03:54.110 --> 03:56.250
And what is the point of this?

03:56.270 --> 03:58.250
Why do we want to make life more complicated?

03:58.850 --> 04:03.920
The thing is, if you have a two-dimensional array, and you access an element in one of the inner arrays,

04:04.280 --> 04:06.380
then that requires a pointer dereference.

04:07.190 --> 04:13.430
Whereas, if you do it this way, you replace the pointer dereference by a multiplication, and on modern

04:13.430 --> 04:17.030
hardware, multiplying is much faster than dereferencing a pointer.

04:17.540 --> 04:23.210
So if you have an application which is performance critical, like games programming or scientific computing,

04:23.210 --> 04:26.480
for example, then this can be a useful trick.

04:27.050 --> 04:32.480
On the other hand, if you want nice, simple notation, then you can use a two-dimensional array instead.

04:34.890 --> 04:36.510
So let's quickly try this out.

04:36.990 --> 04:41.870
Here is our flattened-out, two-dimensional array. It is now a one-dimensional array.

04:42.900 --> 04:48.390
We are going to get the element in row one, column two, and then we are going to print out all the elements

04:48.390 --> 04:48.720
again.

04:49.170 --> 04:55.800
So, instead of having the extra pointer dereference, we calculate the position of the element within

04:55.800 --> 04:56.280
the array.

05:00.810 --> 05:01.320
And there we are.

05:01.560 --> 05:02.850
So we get the same results again.

05:03.690 --> 05:04.020
Okay.

05:04.020 --> 05:05.130
So that is it for this video.

05:05.490 --> 05:06.300
I will see you next time.

05:06.480 --> 05:08.390
Until then, keep coding!
