- Let's look at a different type of array.
This time it what we call a two-dimensional arrays
and let's see what this mean.
Two-dimensional arrays are essentially tables
or matrices that have any number of columns and rows.
For example, this is a table that has two rows
and three columns, hence the definition
of the array is table[2][3].
We initialize the array following the same prinicple
as the one-dimensional array except we have
to provide two sets of numbers within curly brackets.
Because we have two rows and three columns,
this means there's going to be two sets
of numbers in curly brackets.
In each set, there's going to be three numbers.
Other than that, the principle is the same.
The other thing to note is when we refer
to a specific element of the array,
we have to provide two indexes,
one for the rows and one for the column.
In this example, if I want to refer to row one, column two,
I would have to type table[1][2].
Since we have two rows, row number one is actually row zero
because if you recall the indexes in
the array always start at zero.
So, we have two rows, row zero, and row one.
And we have three columns, column zero,
column one, and column two.
Therefore, position [1][2] in the array
is row one, column two.
Row one it'ss actually the second row
and column two it's the third column
which means the number 872 lists which to code
and learn a few more things about this.
The same arrays we just saw is now
being initialized in line 22 with the same exact numbers
and we a're going to print in line 28
the same location [1][2].
There's nothing new here.
What's new it's in the way we are printing
the array to look like a table.
I'm going to run this first so we can have a visual
of how this works and then I'm going to explain a few things
because some of those are not trivial.
So, as we said already, row 1,
column 2 contains the number 872.
If you look at line 24, this is row 1, column 2.
Below is the output of printTable function
which indeed looks like the array.
Let's take a look at how this happened.
First, my printTable requires one argument
because it has one parameter.
Look at an interesting thing.
Recall that when we had a one-dimensional array,
we simply said the array name and then brackets.
When you have a two-dimensional array,
in this case, in line 35, we using int table.
For the rows, it's open brackets
but for the columns, this is not.
This is not a peculiar thing.
It's the way memory is being allocated by
the C compiler when you want to use an array.
We will discuss the reasons why is this way
in the upcoming video when we talk
about pointers and memory management.
For now, let's just accept that if you want
to refer into a function parameter
to a two-dimensional array, you have to specifies
the row components of the array
in open brackets with no value inside,
and the columns you have to specify a number.
Moving on, line 36 is just a label
so we know that to print in the table.
And now we're starting loops.
You already know that in order to print
an array you have to use the four loops.
Well, if we have two dimensions,
we are going to use what is being called as a nested loop.
We need one loop to print all the rows
and for each one of those rows,
we need another loop to print all the columns.
That's why we have the inner loop
in line 38 through 40 that prints the columns
and the other loop on line 37
that ends in line 32 that prints the rows.
So, we have the outer loop runs number of rows time.
The inner loop runs number of columns time.
And eventually when we print table[i][j]
because those are the indexes
for rows and columns respectively
we are ended up with a table.
Finally, two more things.
Line 39, printf("%4d, because I happen
to know that the maximum element
in the array has three digits,
I'm going to allocate four spaces to each element
of the array in order to have the array displayed correctly
and all the numbers aligned.
Second thing to note is line 41, printf.
The reason we need that is still inside the loop four rows.
Each time I finish printing a row,
I have to advance to the next line so
that the rows are printed one below each other.
And that's it.