OK, so now that we learned about two dimensional arrays, we are going to kick it up a notch by going
over in dynamic 2D arrays.
You may think right off the bat that, oh, I have to do a dynamic to derail I got to do is put a pointer
in a new keyword.
Well, it's not going to be quite that easy, unfortunately.
So let's go ahead and see what it would take.
You can go ahead and try that, just change your normal 2D array to just have a pointer and use the
new keyword.
Run it and see what happens.
You probably notice an error.
So let's go ahead and get into and see how we actually do this.
So we're about to get a little bit and next level with the pointers.
So this is how we did a single dimensional dynamic array.
So one day.
You just put this into your pointer here, and just because we're doing out of integers, you could
do it or some other data type.
But we called it Myra, which is going to be a pointer to a new heap allocated array of type integer.
So there's actually a few choices to go about making a dynamic 2D array.
First, we're going to discuss a way that involves some double pointer actions.
So not just one, but it's going to be kind of like a.
It's not necessarily a double pointer, but we're only using two asterisks rather than one, so we're
going to have to really break that down clearly so you can understand.
So it doesn't become too confusing being like it's just some special crazy pointer, you know, or something
like that.
But we'll go ahead and break that down.
I'm not going to go over the other way to do it in this lecture.
I'm and I think there may even be.
There's even more than two ways I think there's like three or three or four ways or something, but
there's two ways that I want to go over.
And in this lecture, we're going to focus on this one with the kind of double.
Pointer here, where we actually still make two dimensional arrays so we can actually make an array
that stores pointers to other arrays.
So that would look a little bit like this.
So here is the first dimension array and the items or elements of this array are pointers.
It point to another era, so here and here and here, the type is a pointer to another array.
So you should also remember that the initial outer layer has to be a pointer is, well, if we want
to put it on the heat.
So not only are we storing pointers to arrays inside of the array, but we also have to have an initial
variable that is a pointer that points to some.
You know, it holds some address where the initial array, which is this thing I'm circling right here,
is stored.
So this is the syntax, this is what it would look like, we have this end star star and I'm using our
multiplication table example, so more table equals new into star a size three.
So let's go ahead and break this down because it's probably a little confusing looking here with all
the stars and whatnot.
So this first part, let's focus on it piece by piece.
So in star, this is basically talking about the data type the initial pointer is going to point to.
So what is the initial pointer pointing to?
Is pointing to this data type, which is.
An integer pointer, why is an integer pointer, because it's an array of integers here.
So the next thing is our actual pointer variables, so we need this pointer variable that has the memory
location for this initial array, so to point to the starting byte there.
Was that contiguous memory?
So this would be our table pointer.
Next, we're just saying with the new keyword, of course, that it's going to be dynamically allocated
on the heap.
This next in star is similar to one we just use end, but what he's saying is the data type of this
initial ray is and a pointer that points to integers right in array of integers specifically.
But that's kind of said right here that it's an array.
So it's just pointing to type integer pointer, right?
Then this is the size of the initial array.
Three, right.
So the initial size is three.
So I explain this and that kind of goes through all of this right here.
But what about this part?
Where does this fall into the picture?
We actually haven't done this part yet.
We need to make this part right here.
And what we need to do is we need to fill that initial array.
This right here, which is this right here, we need to fill it with arrays one at a time.
So one by one.
So we're going to have to make this array in this array and this array, and we're going to have to
make sure that our pointers in here point to these arrays and how we're going to do that.
We can do it with a good old fashioned for a loop.
So you notice here we are going over the number of rows, which in this case will be three, right?
So rows will be three in this example.
And for each one of these right here, you notice we will do this mult table I.
So that's going to reference this right here.
So, Mort, table, I basically said, what table, sorry, what table index I is referencing.
This guy first disarray right here and giving us this guy right here.
And it's also referencing this so we can use this and store that there.
It's similar to us doing something like this like.
This is how we would just make one.
One dynamic away array, right like this right here, we would say, like in Star Myra equals new in
three.
So notice that basically you can think of this replacing this right here, except what we want is not
a new variable per say.
We want to do reference this guy right here, this pointer right here, and we want that pointer to
point to a new.
Array of integers of size three, which is this what with the green box around it right here?
So hopefully that makes sense, we kind of did reference the this example, I was looking at the first
thing.
Right.
We're setting up the first thing.
So I'm pretending that we're going through the first iteration of the Loop and I equals zero.
Let's go ahead and reference the 0th position of this array right here, which is this thing right here.
Once we reference it, we can then assign a value to it.
What is that value?
It is this new and dynamic array of integers of size three.
OK, and you would do the same thing for these other ones, you know, one table I is going to be one
the next generation.
So it would reference this and then we could set it to a new array of size three.
One thing to note is that I just put these numbers in here for just referring to our multiplication
table, but these numbers aren't going to be in there in this example, right?
It's just going to be some empty space, right?
Because we're just creating a new array.
But we're not putting the values at the right spot yet.
So what I'm going to have you do is to put the values at the right spot.
You've already made a program that does that right, but now that you know how to do dynamic to arrays,
I challenge you to change the multiplication table program that we saw in the previous lecture into
a program that can accept a table size from the user.
So, for example, not three by three, but maybe four by four or five by five, seven by seven, something
like that, 10 by 10.
And then you're going to want to make a dynamically allocated more multiplication table of that request
in size than you should, of course, print it out just like you did in the previous program.
So if you need to, you can go back and look at the you go back in the video and kind of listen to my
explanations and look at the slides.
But this should tell you how to set it up, and you should be able to adapt the program to be able to
use this type of dynamic 2D array.
Right.
So this kind of shows you how you would handle it for something no size three, but imagine of a variable
size, right?
And we can do that since it's a dynamic array.
So at this point, you should pause the video and try your best to sell this program on your own.
OK, so hopefully you pass the video and try that.
I'm going to go ahead and go over a potential solution.
So let me head here.
So here's kind of what our old program looked like, right?
We had just this loop right here that made the mold table, which was our 2D array, of course, I accidentally
deleted it here.
So we run it and it gives us this right here.
So this is kind of what you should have had in the last program, but now we're going to change that
into a dynamic array.
So what we're going to do is start off with that syntax that we saw, right?
So it's going to be in star star.
I'm going to call it more table again and I'll say equals new and star.
And it's not going to be three.
We need to take that in from the user, right?
So how will we get a table size?
And we see in the table size.
And then we're going to make this table size.
So what we're going to do, then, is we need to make a new array, so I'm going to make another I live
here, right?
We need to make an array for each section of our initial array, right?
Each position is going to be a pointer to another array of integers.
So what I'm going to say is just go for and I equals zero.
I is less than table size.
I +, + +.
I will go in here and I am actually going to do something like this.
I will say Mult table, I equals new.
So remember this D references our multi table and it's going to then leave us with an.
A pointer that points to entered integers, which is going to be OK for us to assign an array to that
of type integer, right, so we'll saying you int.
And here we're just going to put our table size right because we want the rows and the columns to be
the same.
So we'll have kind of.
That many rows and that many columns, right?
So let's go ahead and continue on with this.
So.
What we need to change here is not have this be a three anymore, but instead it should be less than
table size, right?
And then here, we should say less than table size.
This should be oops, I didn't mean to do that.
There should be see, this should be table size on less than table size here and less than table size
here.
So we're still able to add things into our multiplication table because this basically the references
because of these right here, the subscript operator.
So this old reference that first pointer and then it will reference the second pointer into the correct
position of the second dimension array.
And then we just want to put the product there, correct?
So this will do the same thing, you know, it'll actually do reference ID so we can print it out as
well.
So let's go ahead and save this, compile it and test it out.
So I'm going to say it clear, I will compile it looks like it compiles, OK, let's run it and I'm
going to go ahead and just put four here.
And you notice that this table size of four gives us this extra dimension, so we basically have zero.
It's basically zero one two and three zero one two and three.
So at the end here we have three times three is nine.
Two times, three, six, one times three is three.
One times one is one, one times two is do all that good stuff, right, you don't use four to 10,
three six.
So pretty cool, right?
And we can increase this size, though.
So what if I was to go?
Let me run it again, and I'd put like 12 or something like that.
We'll call it print it out.
You know, a lot of stuff, but this looks like kind of messy and out of place down here, right?
So see how it's kind of offset.
So you might notice that.
You know, things aren't printing out so clean.
And of course, I mean, you might be able to like add some more spaces in here, and you might think
that that makes it a little bit nicer.
Like if I was to do that and run it again with 12.
Then it's like some stealth lines of a little bit better, but it's still is like pretty gross here,
right?
So what can we do about this?
Well, I'm actually going to show you a way to align things a lot nicer in the next video.
So we'll go over that, and we will also maybe go over another way to create two dimensional dynamic
arrays.
So hopefully this was educational for you.
And if you didn't figure it out, always, of course, don't worry.
Just go back, try and type this from memory now instead of just reading it.
Take a look.
And then each time you write a piece of code, try and stop and see if you understand what it is that
you're writing and what it really means and why we use it.
And if you need to go ahead and refer back to the lecture, you can look at C++ documentation.
Or whatever you need to do.
All right, so with that, I will go ahead and see you in the next lecture.