Hello and welcome.
In this lecture I want to talk about multi-dimensional arrays.
Up until this point, if you've thought of an array, you just
said an array is a variable that can store many types of data.
It can get more complicated than that.
You can have basically many dimensions of an array.
So if you just have a linear array which is what we've discussed so far,
that's called a single dimension.
And we know it's a single dimension because it just has
one brackets with one size.
The c language allows arrays of any dimension to be fined.
So this is kind of thought of like a nested array, an array of an array.
So two-dimensional arrays are the most common.
But you can actually do three-dimensional arrays,
four-dimensional arrays, five-dimensional arrays, but there's
not many use cases for 3 4 and 5.
Really, you're either going to have a single-dimensional array
that you use in your program or two-dimensional array.
two-dimensional arrays are common because they essentially represent
rows and columns in a spreadsheet, that's how you can kind of
visualize it or a matrix or a table.
That's a two-dimensional array, you have rows and you have columns.
So you can store more data than a single dimensional
array by specifying the sizes.
But it's very applicable because it looks like a matrix.
And there are applications that you may want to write that has this
concept of a matrix, specifically a mathematical application
would be most relevant here.
But you can use to do two-dimensional arrays for many different things.
One example is a matrix.
But again, visualize it as a table.
Two-dimensional arrays are declared the same way that
a one-dimensional array is.
The only difference is you now have another set of brackets.
So in this example, we're declaring an array named matrix of type int
which has 4 rows and 5 columns.
That means you can store 20 elements inside that two-dimensional array.
So you should know how each dimension is specified with its
own pair of square brackets.
that's how you can easily spot how many dimensions an array has.
If you had 3 brackets there, that would be a three-dimensional array.
If you have 4, it would be four.
This one has 2 so it's 2.
And up until the earlier lectures only had one, so we
knew that was a single dimension.
So look at the brackets, pay attention to that.
Again 4 rows, 5 columns, total of 20 elements.
To initialize a two-dimensional array, it's going to be initialized in the
same manner of a one-dimensional array, same type of syntax,
you're just going to add more.
So when you list the elements for initialization, the values
are going to be listed by row.
And the difference here is that when you put the initial values
for each row, you're going to do it between braces, curly brackets
and then you're going to enclose all the rows between braces.
So it's going to look something like this.
If we create an array of numbers, which is of 2 dimensions, 3 rows
and 4 columns and we want to initialize it, all you have to do
is put inside curly brackets the entire array and then add inner
nested curly brackets for each row.
So here we're initializing values 0 sub-0 to 10, 0 sub-1 to 20, 0
sub-2 to 30 and 0 sub-3 to 40.
And that's how you access multi-dimensional arrays too.
You access them with the row in the column.
You just have to remember that it's 0 based.
So the upper left hand which would be row 1 column 1 would
be 00 and that'd be equal to 10.
Row 1 column 2 would be equal to 20, but you would
access it as numbers 0 sub-1.
Because the first array contains 3 rows you access it that's the number
you specify for the row number and then the column number is the next
specification for accessing the array.
But individualization is pretty simple, just separate things
by comments with the brackets.
And accessing them, you would access it just like you access a normal array
except you're now you're accessing this concept of a row in a column.
So the use of the inner pairs of braces is actually optional, but
you should use it for readability.
If you didn't provide those braces, you could actually just provide
10 20 30 40 15 25 35, all just consecutively, but it's very hard to
visualize what you're initializing.
As a programmer, readability is very, very important because it
increases the quality of the code it's easier to maintain the code.
When I look at this right here, I can easily identify, especially
with the comments, what I’m initializing to, what values and
what rows are going to initialize to.
If I had that as a single line just consecutive numbers, I
would get lost really quick.
So although the inner brackets are not necessary, use them.
As with a one-dimensional array, it's not required to have the
entire array be initialized.
You can just initialize certain areas.
So in this example, we're creating two-dimensional array
with 4 rows and 5 columns.
We're only initializing the first 3 elements of each row.
So row 1 column 1 is 10, row 1 column 2 is 5 and row 1
column 3 is a negative 3, and the same for the second column
and so forth 900 32 31 008.
For the ones that we didn't set the 4th and fifth values of each
row, those are actually going to be set to 0 automatically
because we left them out.
And in this case, you actually have to provide the curly
braces, the inner ones.
Because here if we did it, the initialization
would be all screwed up.
Because if you didn't have the curly braces, it would
just do it consecutively.
So what would happen is this would actually initialize
the first 12 elements.
So it would initialize all the elements in the first row, all the
elements in the second row and then the first 2 elements in the third
row and then the rest of the elements in the third row, the rest of the
elements in the 4th row would be 0.
Again, this is a two-dimensional array of 4 rows and 5 columns.
It would just do them consecutively.
So don't forget the braces if you're if you only want to specify
certain initialization elements.
You can also do designated initializers.
Remember, this is the C99 concept.
That's optional in C11 where you can specify specific
indices to be initialized.
So you can use subscripts in the initialization list, just like in a
single dimensional array if you want to do a designated initializer, you
would just do something like this.
So here we're declaring our matrix array of 4 rows and 3 columns,
but we're actually specifying that 0 0 is equal to 1, 1 1 is
equal to 5 and 2 2 equals the 9.
So when we say 2 2 again, what we're saying is row 2 which is
really row 3 because we start at 0, column 2 which is really column 3.
Again, visualize this as a table or a spreadsheet.
And accessing the elements start at the top left-hand corner.
But if you understand that the 4 represents the rows and the
3 represents the columns, it's pretty intuitive after that.
So what this is doing is it's initializing the 3 indicated elements
of the matrix to specified values.
But what you should notice here is the syntax.
All the unspecified values here are going to be 0 by default.
So this is only going to initialize 3 values.
Each set of the values that you do initialize are going to be the
elements in a row is between braces.
And then the entire initialization goes between another pair of braces.
So you see that we have the curly brackets on the outer ones
just like normal initialization and then we have the brackets
to access the array inside.
And then we have commas to separate each row and each
set of row values is separated from the next set by a comma.
So the values are separated and also the row indices.
Now just because we said that we have 2 dimensions and we've
been talking about that; you can actually have many other dimensions.
Everything I've talked about with 2 dimensions also applies to 3
dimensions 4 dimensions 5 dimensions.
So if you wanted to declare a 3 dimensional array, you would
do it with 3 square brackets.
So here's an array named box of type int of 10 20 and 30.
You might want to use a three-dimensional array maybe if you
were storing xyz values of some sort, you probably won't need to do this.
Three-dimensional arrays and up are really not common.
As a summary, you can visualize a one-dimensional array as a row of
data, single row, two-dimensional array as a table a matrix or
spreadsheet, in a three-dimensional array, you can visualize as a stack
of data tables, so a stack of tables.
If you needed that much data, it's going to be really hard to
access it and probably confusing.
And anybody that's maintaining this program for a three-dimensional
array or above, it's probably going to get lost trying to figure out
what's going on, but you can do it again and if you're ever going to
need to iterate through any of these one, two, three-dimensional arrays,
you're probably going to use a loop.
And the more dimensions you have in the loop, the more nested
loops you're going to have.
So if you want to process a three-dimensional array, you're
going to have 3 nested loops.
And if you want to process a four-dimensional array, you're
going to have 4 nested loops.
So as far as initializing an array that's higher than 2 dimensions,
again, you're going to apply the same type of thing, you're
just going to extend it a bit.
So a three-dimensional array will have 3 levels of nested
braces with the inner level containing a set of initializing
values for each row, so it would look like something like this.
Here we have a three-dimensional array, 2 3 and 4, a stack
of data tables essentially.
And the first block of 3 rows, we're going to initialize and
the second block of 3 rows we're going to initialize.
So what this is saying is again first block and second block
because we didn't initialize the third and the fourth block,
just the first and the second.
So we have 4 blocks and you can think of as each block
a stack of data tables, each block having rows and columns.
So the first block is going to be initializing just the first 3 rows,
which is all we have is 3 rows.
And then the second block is going to initialize the next 3 rows, but
the third and the fourth blocks are not going to be initialized.
If you want to initialize the third and the fourth blocks,
you'd have more inner braces, curly brackets and so forth.
Same thing applies though for the ones that you do not initialize,
they will be initialized to 0.
But again, you can see how it gets very tedious and
also very hard to follow.
When you want to process the elements in a dimensional array, I mentioned,
you're going to use nested loops.
Again, it's going to correspond directly to the number of dimensions.
And each loop is going to iterate over one array dimension.
So it's going to look something like this.
This is an example of a three-dimensional array.
You have an outer loop and then you have 2 inner loops.
The outer loop is going to be the first outer array, so the outer
loop would essentially go through the rows, the second one will go
through the columns and the third one will go through the blocks.
Again, what you should be noticing is wow, this gets really hairy
and really tedious, really fast.
So try to avoid three-dimensional four-dimensional arrays and
I don't even see many use cases for it, one and two are
really all you're going to use.
But understand multi-dimensional arrays are a possibility in c, a way
of storing more data and accessing the same name with many data elements. Thank you.