OK, everybody.
Welcome back to another lecture in this lecture, we are going to be continuing on with the nested looting,
but what we're really going to talk about is 2D arrays and matrices, which we will have to use some
nested loops to be able to deal with these things.
So let's go ahead and get into it.
So I'm just going to go ahead and pretend like nobody knows what a matrix is.
A lot of you might already know what this is for math classes and things like that.
And what we're really talking about, though, is a two dimensional array and that is a grid and a multiplication
table is a good example of that unit as we have a handful of columns and some rows, and then we have
the corresponding correct products in here at the multiplication of these columns and rows.
So this concept of rows and columns and matrices, we are specifically talking only about 2D matrices
in this lecture.
And if you've taken some math classes before the deal with matrices, you might be thinking that, you
know, of course, matrices don't mean that it's two dimensional.
It could be, you know, in dimensions, any type of any number of dimensions, which you also do deal
with those a lot in computer science and programming.
But that's not what we're looking at right now.
So for the sake of this lecture, just imagine only dealing with a two dimensional matrix.
So we need comfortable with this concept of the rows and the columns.
If this is something you're not familiar with, the green bars are considered rows and the red bars
are considered columns.
So.
This is the way that the Matrix looks in array form.
So think about having an array like you've made before, an array of integers or something like that.
So that array what we would do if we were relating it to what we see here.
So we'd look at these grey brackets right here.
So this open gray bracket in this close gray bracket or the actual array right in the outer layer of
the array, the elements or items inside of the array are the rows.
So rather than having an integer.
The first item or at the 0th position in the array is this whole row here between the green square brackets.
So our rows are denoted by the green square brackets.
So this is row zero.
It is the first item in the array at position zero.
This is row one.
It is at position one.
It is the second item in the array.
This is the road to which is the position to it is the third item in the area.
So are items just so happen to be another array?
So our outer layer is, you know, this whole matrix.
It's this array right here in this array has three things in it, but those three things happen to be
arrays.
And so this is where we get into columns, when we're looking at the positions of each one of these
arrays, which are rows.
We are talking about columns being the positions, so we have our colleagues to noted and read here.
So if I refer to the first item in our whole array, so we're talking about the outermost layer, which
is the grey brackets.
If I refer to the first item, I am talking about rose zero, right?
Now, if I am wanting to talk about the first item in row zero, I am referring now to a column, so
it is column one, right?
That is the first, uh, position of this array right here.
So that's where this other dimension comes in, the first dimension is basically Rose.
That is the initial elements that you're looking at when you would index it just like the race you've
done so far.
You only had one index, right?
You use the index operator and you just indexed into one of the positions, let's say, position zero
that would grab you all this, that I'm circling here, but we're not done yet.
If we want to get an actual value, we now need to think about the position inside of this row and that's
where the red values come in.
These are the positions, they are the columns.
So let's take a closer look at what this exact example would look like when we're trying to raise some
code so we would declare an array.
I'm just calling it an array of integers called my array.
And you notice that it has.
Two sizes here, and you might be able to guess that that's because we need to specify the size of the
rows and the size of the columns, the number of rows and the number of columns.
And that's what these two numbers mean.
Normally you would just block this off and you would only have this right.
You'd say Antminer A3 and you'd be like, OK, I just made an array that has three things in it.
At position zero, one and two, respectively, if you add this second dimension here with another one
of these kind of the index operators, but now they're being used to set the size, of course, when
you declare an array.
Then you're specifying the columns as well, you're adding this new dimension and it becomes a matrix.
So you notice that it has three rows, right, so the initial array has three things in it, and then
we say each one of those things in the array also has three things in it one, two three.
So the first layer?
Right?
Three things in it.
That's one to three.
The second layer, each one of these things has one two three or one two three or one two three, respectively.
So that is how we can, you know, take a look at what we've written so far and imagine how it would
be declared it would be something like this.
Now, let's think about how we would access something in this array, will it be this kind of the same
idea is how we declared it.
We still need to refer to the both the row and the column and specifically in this order, of course,
we need to first index into the first layer, which is the rose.
Right after we get the correct row that we want, we can then choose the column from that row, right?
So what would this be?
Well, let's first look at the row, right?
It's row zero.
So that means that it's the zeroth position in this outer array, which gives us this right here, right?
This is the first thing in there is the zeroth position.
Once we have this now we have an array right when we do Myra's zero.
It now gives us this right here.
This happens to be another array.
So now we must index this second layer array.
So how we can do that, we're going to say, Well, I want the thing at position one.
So we have position zero, position one.
So that's going to be right here.
And so what we can do when we look at our grid is we can narrow it down to be this exact position right
here, we draw a line out to how we draw.
So we come here and we know what's on this row and we know it's here and so we can draw a line right
here to this value.
And that is what this syntax returns to us.
It will be zero.
So now that we know how to index it, how would we populate this to the era?
So let's say we just declare it when we declared it before.
You know, obviously this wouldn't be filled with stuff.
All we said was my we are declaring an array that is size.
Three rows, three columns.
It's actually going to look like this when we declare it's an empty right, it's an empty structure
of, you know, it's just waiting for stuff to be populated and set aside the space.
But we haven't put anything in it yet.
So how would we populate this 2D array?
Well, you probably can take a guess since I mentioned it kind of before we're going to use a nested
loop to populate it.
And so I'm not going to give you the straight up code.
I'm going to give you something called pseudo code, which is only partial code.
It's kind of a mix between, you know, written spoken language and code.
So that way, I think I can explain the concept to you.
You can remember back to how you have learned to use the nested loops.
And then we will we will try and solve this together.
All right.
So let's look at how the populating the 2D array would would work out.
So you see a for loop in a nested for loop inside right here, right?
And we're assuming that we've already declared this in the code.
This is just the populating the array part of the code.
Of course, you'll have other stuff in your program, right?
But we're not mentioning that we're focusing on this right here in the part of the code that populates
the two dimensional array.
Outer layer, rib, outer layer, loop, say outer layer loop is going to be looping over the number
of rows.
How many rows is going to be three?
So I'll let you think about that, how you'd be able to code that.
Then we enter that.
And for each one of the rows, we now need to focus on a column for each row member we're talking about.
Each row is a it's an array in itself.
And now we need to focus on inserting values into each one of these arrays.
Right.
So we do that with the inner loop and then we loop over the number of columns, right?
Then inside this inner loop is where we actually insert the values, and what we do is we index my my
array with the row and the column just like I showed you before, remember with this row and column.
And then we can calculate our product and assign it to that specific location inside the two dimensional
array.
So let's go ahead and step through this.
So first time that I hit this line, it's going to be when the row is zero and the column is zero.
And so we put R0 there, right?
Next, it goes back up to the fore, and we're still going to loop because we're not done yet, right?
We're looping for, you know, something like this, lying is the column.
It's less than three and we go down here and we add the next one, right?
We increment, we add the next one.
Now we notice that we're over our size for the row, so we're going to be done with this inner loop,
so we go back into the outer loop, we're not done with the outer loop yet, though we still have some
more spots to fill, right?
So we go back to the inner loop, we fill each spot one to three.
Then we go back to the hourly, if we go to the next row down here in our loop, we fill column one
column, two column three, which is position zero, one and two.
We notice we're done with this inner loop and now we've filled everything.
We are done with the outer loop, so we are completely done filling the grid for the multiplication
table.
So with that, let's see if you can write this as a full program.
So I believe that with what you've seen so far, you are capable of at least partially figuring out
how to rate this program, if not all of it.
So try and use what you've seen so far.
If you need to go back in the lecture and look at some things to kind of refresh and what we just talked
about and you know, if you if you've forgotten kind of a few the things on the previous life, go ahead
and do that.
But you what you want to do is make a full program that not only fills this multiplication table of
three by three.
You also want to print it out.
So here's some questions you're going to have to answer for yourself.
How are you going to print it out to make it look like the table in the slides?
And then inside of that question is kind of like when should you print a number versus printing a new
line which is slash in or in in the line, you know, using end lines and go to a new line?
Another thing that may or may not seem straightforward to you is what things are you multiplying to
field values in the table?
So you want to make it?
Not necessarily.
It doesn't have to print the row labels and column labels, and it doesn't need to print these square
brackets.
But it should look something like if you just look at the positions of all these values, you know,
it should be like zero zero zero with spaces in between zero zero zero.
Go to another line zero one to go to another line at the zero to four.
All right.
So that is what you should be printing out.
So go ahead and pause the video, and I think this is very important, you should really push yourself
to try and figure this out before I go over the solution.
This is really in the end, what's going to get you the most out of this course?
The more that you attempt these things on your own without just going straight to the solution, the
better off you're going to be in the end and the more comfortable you're going to be when you're trying
to put everything together and really trying to get yourself out there and get a job and achieve your
goals and all this stuff.
Programming is about problem solving in the end.
All of these other syntax, things and tools they can all be learned or taught by.
A lot of people are really anyone, you know?
But what I'm trying to do is just show you that and get you to use it and solve your own problems because
problem solving is something that's harder to teach.
So the more you try it on your own and push yourself, the better you'll be in the end.
So with that, go ahead and pause the video.
See if you can make a full program that fills this multiplication table.
It only has to be a three by three three rows, three columns and then go ahead and print that to the
table out.
OK, so now I'm going to go ahead and get out of here and go on to the ED and we're going to go over
the solution.
So I've already kind of set up a program here that's just empty.
So what are we going to do right off the bat?
Let's go ahead and make our array right.
So I'm just going to call this moss table.
It's going to be a three by three array or matrix.
Then I can get into filling it right off the bat.
So I'm going to make a nested for loop just like we were talking about or I'm going to say four and
are four rows equals zero.
Row is less than three because we're looping over the positions zero and one and two, right?
Five plus plus.
Then I'm going to start another loop and I'm going to say and C equals zero.
C is less than three as well four columns.
And I'm going to say C++.
Pretty punchy right there.
C++.
And now what we're going to do is do our multiplication part, right, so I'm going to say more stable
index the row, then index the column and this is what we're counting over, right?
So when let's take the first iterations, for example, so we enter this four loop gets set to zero
for the start.
We enter this four loop.
See, it's set to zero for the start.
We hit this line.
It says more table zero rows zero column zero.
What is that going to be?
Well, it's just simply going to be zero times zero right that upper left corner.
Each time is just going to be the row times the column.
So that's what I'll do.
I'll St Row Times column.
So what this will do is it will, you know, put this in the correct location.
So we're filling our table like this, and, you know, of course, what we could do is almost just
print this out all in one loop.
I show you how sorry all in one nested loop structure like this, we wouldn't have to do it again.
So I'd do that afterwards.
But let's separately fill our table for now and then we will print it out.
So this is all we need to completely fill our table.
So now I'm going to write another kind of nested form of structure like this.
It's going to be exactly the same.
So I might as well actually just copy this and paste it here.
But now what we're doing is we want to print this out.
So I'm going to say, see out and I'm going to do more table row column and then I'm going to put a
space in between.
So when I'm printing out, remember the inner loop is for printing out the.
Columns for a specific row.
So how do we want to print this out?
Well, let's say we're on rose zero to start with, right?
So our equals zero.
We want to basically print out each column in that row and then we want to go to the next line and then
we want to print out each column in that row and then go to the next line and do the same, right?
So because of that, I'm going to make sure that I don't put it in line here when we're printing each
column for that first or second or third row.
We're just going to print out the numbers with a space.
Once this whole loop has ran for three times and it's done each column for the first row, for example,
then I can print out a new line.
So I'll just see out in line it'll go to the next line.
We will increment our row to the next row, so we go down one row and then we'll run this inner loop
and do each one of the columns for that next row.
Then we can go to the next line and so on and so forth.
So that is why I'm putting the sea out in line after this loop and the inner loop, right?
And inside this, we just print each column on the same line.
So let's go ahead and see how this looks.
I'm just finishing up the function there and I'm going to file save and then I am going to go ahead
and I'm going to compile this and run it and look at that, that prints out our table pretty nicely
there.
So we have this zero zero zero zero one two zero two four.
So pretty nice.
So what we could do is like we wouldn't even necessarily have to fill an array for this.
But it's important to know how to make the matrices and stuff like that because we're actually going
to be starting our first project soon and it's going to involve matrices.
So we're going to need to know how to use these 2D arrays.
But what could actually happen is we could just print this out, so if we didn't care about a matrix,
you know, this program could actually become simpler and we wouldn't even need an array.
We could honestly just go through and we could print out this.
Our times, see, so I could just say, see out our time.
See?
And then I could do a space and like that.
And then I could say, see out in the line.
And if I was to save this and run this, I'm on a clearance and pilots run it.
You notice that it prints out the same exact thing.
So we it's kind of would seem pointless if to, you know, make this 2D array for this specific program.
But the main purpose was having you figure out how to populate this two dimensional array and then know
how to print out from a two dimensional array.
So pretty cool, pretty cool.
I think that what we will do is go over some practice problems with these may be just a few, maybe
one or two.
And I will do that in the next lecture.
And after that, I think that we will be pretty much ready to have you start the first project, which
will be a Tic TAC toe game actually where we're going to make that full game and be able to have multiple
players.
And we're going to be using a table for a Tic-Tac-Toe table, which is going to be an array like this.
I think in the next lecture, we'll go ahead and just convert this program to a dynamic array and then
we'll just mess around with kind of 2D dynamic arrays and stuff like that.
All right.
So with that, I will see you in the next lecture.