1
00:00:01,410 --> 00:00:11,460
OK, so now that we learned about two dimensional arrays, we are going to kick it up a notch by going

2
00:00:11,460 --> 00:00:14,250
over in dynamic 2D arrays.

3
00:00:15,180 --> 00:00:23,340
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

4
00:00:23,340 --> 00:00:24,330
in a new keyword.

5
00:00:25,750 --> 00:00:30,850
Well, it's not going to be quite that easy, unfortunately.

6
00:00:31,360 --> 00:00:34,210
So let's go ahead and see what it would take.

7
00:00:34,240 --> 00:00:42,250
You can go ahead and try that, just change your normal 2D array to just have a pointer and use the

8
00:00:42,250 --> 00:00:43,090
new keyword.

9
00:00:43,210 --> 00:00:45,430
Run it and see what happens.

10
00:00:46,060 --> 00:00:47,740
You probably notice an error.

11
00:00:48,550 --> 00:00:52,240
So let's go ahead and get into and see how we actually do this.

12
00:00:53,800 --> 00:00:56,950
So we're about to get a little bit and next level with the pointers.

13
00:00:58,010 --> 00:01:02,730
So this is how we did a single dimensional dynamic array.

14
00:01:03,470 --> 00:01:04,760
So one day.

15
00:01:05,850 --> 00:01:10,470
You just put this into your pointer here, and just because we're doing out of integers, you could

16
00:01:10,470 --> 00:01:12,780
do it or some other data type.

17
00:01:13,050 --> 00:01:19,800
But we called it Myra, which is going to be a pointer to a new heap allocated array of type integer.

18
00:01:21,500 --> 00:01:26,840
So there's actually a few choices to go about making a dynamic 2D array.

19
00:01:28,670 --> 00:01:35,120
First, we're going to discuss a way that involves some double pointer actions.

20
00:01:35,120 --> 00:01:37,970
So not just one, but it's going to be kind of like a.

21
00:01:39,210 --> 00:01:44,070
It's not necessarily a double pointer, but we're only using two asterisks rather than one, so we're

22
00:01:44,070 --> 00:01:49,720
going to have to really break that down clearly so you can understand.

23
00:01:49,730 --> 00:01:56,100
So it doesn't become too confusing being like it's just some special crazy pointer, you know, or something

24
00:01:56,100 --> 00:01:56,580
like that.

25
00:01:56,580 --> 00:02:00,380
But we'll go ahead and break that down.

26
00:02:00,390 --> 00:02:04,740
I'm not going to go over the other way to do it in this lecture.

27
00:02:05,040 --> 00:02:07,500
I'm and I think there may even be.

28
00:02:07,890 --> 00:02:14,160
There's even more than two ways I think there's like three or three or four ways or something, but

29
00:02:14,160 --> 00:02:15,900
there's two ways that I want to go over.

30
00:02:15,900 --> 00:02:20,400
And in this lecture, we're going to focus on this one with the kind of double.

31
00:02:21,340 --> 00:02:32,140
Pointer here, where we actually still make two dimensional arrays so we can actually make an array

32
00:02:32,530 --> 00:02:37,330
that stores pointers to other arrays.

33
00:02:38,630 --> 00:02:40,760
So that would look a little bit like this.

34
00:02:41,090 --> 00:02:49,550
So here is the first dimension array and the items or elements of this array are pointers.

35
00:02:50,580 --> 00:02:59,040
It point to another era, so here and here and here, the type is a pointer to another array.

36
00:03:02,700 --> 00:03:09,450
So you should also remember that the initial outer layer has to be a pointer is, well, if we want

37
00:03:09,450 --> 00:03:10,350
to put it on the heat.

38
00:03:10,560 --> 00:03:17,550
So not only are we storing pointers to arrays inside of the array, but we also have to have an initial

39
00:03:17,550 --> 00:03:20,970
variable that is a pointer that points to some.

40
00:03:20,970 --> 00:03:25,620
You know, it holds some address where the initial array, which is this thing I'm circling right here,

41
00:03:25,620 --> 00:03:26,250
is stored.

42
00:03:28,860 --> 00:03:35,910
So this is the syntax, this is what it would look like, we have this end star star and I'm using our

43
00:03:35,910 --> 00:03:43,200
multiplication table example, so more table equals new into star a size three.

44
00:03:44,250 --> 00:03:50,240
So let's go ahead and break this down because it's probably a little confusing looking here with all

45
00:03:50,260 --> 00:03:51,810
the stars and whatnot.

46
00:03:52,950 --> 00:03:56,570
So this first part, let's focus on it piece by piece.

47
00:03:56,580 --> 00:04:02,850
So in star, this is basically talking about the data type the initial pointer is going to point to.

48
00:04:02,880 --> 00:04:05,500
So what is the initial pointer pointing to?

49
00:04:05,820 --> 00:04:07,800
Is pointing to this data type, which is.

50
00:04:08,700 --> 00:04:13,900
An integer pointer, why is an integer pointer, because it's an array of integers here.

51
00:04:16,550 --> 00:04:24,020
So the next thing is our actual pointer variables, so we need this pointer variable that has the memory

52
00:04:24,020 --> 00:04:30,050
location for this initial array, so to point to the starting byte there.

53
00:04:31,060 --> 00:04:32,470
Was that contiguous memory?

54
00:04:33,220 --> 00:04:35,900
So this would be our table pointer.

55
00:04:38,170 --> 00:04:42,190
Next, we're just saying with the new keyword, of course, that it's going to be dynamically allocated

56
00:04:42,190 --> 00:04:42,670
on the heap.

57
00:04:45,830 --> 00:04:51,860
This next in star is similar to one we just use end, but what he's saying is the data type of this

58
00:04:51,860 --> 00:04:59,210
initial ray is and a pointer that points to integers right in array of integers specifically.

59
00:04:59,220 --> 00:05:02,720
But that's kind of said right here that it's an array.

60
00:05:02,720 --> 00:05:06,710
So it's just pointing to type integer pointer, right?

61
00:05:08,450 --> 00:05:10,880
Then this is the size of the initial array.

62
00:05:13,220 --> 00:05:14,030
Three, right.

63
00:05:14,630 --> 00:05:16,490
So the initial size is three.

64
00:05:19,630 --> 00:05:24,130
So I explain this and that kind of goes through all of this right here.

65
00:05:24,880 --> 00:05:26,800
But what about this part?

66
00:05:26,830 --> 00:05:28,480
Where does this fall into the picture?

67
00:05:29,080 --> 00:05:31,200
We actually haven't done this part yet.

68
00:05:31,420 --> 00:05:34,390
We need to make this part right here.

69
00:05:35,200 --> 00:05:39,040
And what we need to do is we need to fill that initial array.

70
00:05:39,340 --> 00:05:46,720
This right here, which is this right here, we need to fill it with arrays one at a time.

71
00:05:46,720 --> 00:05:47,770
So one by one.

72
00:05:47,780 --> 00:05:52,240
So we're going to have to make this array in this array and this array, and we're going to have to

73
00:05:52,240 --> 00:05:57,240
make sure that our pointers in here point to these arrays and how we're going to do that.

74
00:05:57,250 --> 00:05:59,530
We can do it with a good old fashioned for a loop.

75
00:06:00,230 --> 00:06:06,910
So you notice here we are going over the number of rows, which in this case will be three, right?

76
00:06:07,510 --> 00:06:09,850
So rows will be three in this example.

77
00:06:11,230 --> 00:06:18,940
And for each one of these right here, you notice we will do this mult table I.

78
00:06:19,540 --> 00:06:23,830
So that's going to reference this right here.

79
00:06:25,640 --> 00:06:33,140
So, Mort, table, I basically said, what table, sorry, what table index I is referencing.

80
00:06:34,180 --> 00:06:37,780
This guy first disarray right here and giving us this guy right here.

81
00:06:38,350 --> 00:06:43,510
And it's also referencing this so we can use this and store that there.

82
00:06:43,780 --> 00:06:46,690
It's similar to us doing something like this like.

83
00:06:48,180 --> 00:06:50,250
This is how we would just make one.

84
00:06:51,570 --> 00:06:58,680
One dynamic away array, right like this right here, we would say, like in Star Myra equals new in

85
00:06:58,680 --> 00:06:59,220
three.

86
00:06:59,850 --> 00:07:06,540
So notice that basically you can think of this replacing this right here, except what we want is not

87
00:07:06,540 --> 00:07:08,850
a new variable per say.

88
00:07:09,210 --> 00:07:16,500
We want to do reference this guy right here, this pointer right here, and we want that pointer to

89
00:07:16,500 --> 00:07:18,660
point to a new.

90
00:07:20,790 --> 00:07:27,270
Array of integers of size three, which is this what with the green box around it right here?

91
00:07:28,550 --> 00:07:34,470
So hopefully that makes sense, we kind of did reference the this example, I was looking at the first

92
00:07:35,130 --> 00:07:35,730
thing.

93
00:07:35,760 --> 00:07:36,150
Right.

94
00:07:36,450 --> 00:07:37,620
We're setting up the first thing.

95
00:07:37,620 --> 00:07:41,730
So I'm pretending that we're going through the first iteration of the Loop and I equals zero.

96
00:07:42,420 --> 00:07:50,220
Let's go ahead and reference the 0th position of this array right here, which is this thing right here.

97
00:07:51,030 --> 00:07:54,870
Once we reference it, we can then assign a value to it.

98
00:07:54,900 --> 00:07:55,820
What is that value?

99
00:07:55,830 --> 00:07:59,910
It is this new and dynamic array of integers of size three.

100
00:08:02,640 --> 00:08:07,740
OK, and you would do the same thing for these other ones, you know, one table I is going to be one

101
00:08:07,740 --> 00:08:08,790
the next generation.

102
00:08:08,790 --> 00:08:13,950
So it would reference this and then we could set it to a new array of size three.

103
00:08:15,780 --> 00:08:23,130
One thing to note is that I just put these numbers in here for just referring to our multiplication

104
00:08:23,130 --> 00:08:27,110
table, but these numbers aren't going to be in there in this example, right?

105
00:08:27,120 --> 00:08:29,880
It's just going to be some empty space, right?

106
00:08:30,600 --> 00:08:32,700
Because we're just creating a new array.

107
00:08:34,050 --> 00:08:37,200
But we're not putting the values at the right spot yet.

108
00:08:39,330 --> 00:08:45,510
So what I'm going to have you do is to put the values at the right spot.

109
00:08:45,690 --> 00:08:52,320
You've already made a program that does that right, but now that you know how to do dynamic to arrays,

110
00:08:52,320 --> 00:08:59,010
I challenge you to change the multiplication table program that we saw in the previous lecture into

111
00:08:59,010 --> 00:09:02,810
a program that can accept a table size from the user.

112
00:09:02,820 --> 00:09:08,730
So, for example, not three by three, but maybe four by four or five by five, seven by seven, something

113
00:09:08,730 --> 00:09:09,990
like that, 10 by 10.

114
00:09:10,890 --> 00:09:16,410
And then you're going to want to make a dynamically allocated more multiplication table of that request

115
00:09:16,410 --> 00:09:20,130
in size than you should, of course, print it out just like you did in the previous program.

116
00:09:21,060 --> 00:09:27,690
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

117
00:09:27,690 --> 00:09:29,760
explanations and look at the slides.

118
00:09:30,510 --> 00:09:38,160
But this should tell you how to set it up, and you should be able to adapt the program to be able to

119
00:09:38,160 --> 00:09:41,610
use this type of dynamic 2D array.

120
00:09:42,700 --> 00:09:42,970
Right.

121
00:09:42,990 --> 00:09:48,690
So this kind of shows you how you would handle it for something no size three, but imagine of a variable

122
00:09:48,690 --> 00:09:49,380
size, right?

123
00:09:49,980 --> 00:09:52,020
And we can do that since it's a dynamic array.

124
00:09:53,550 --> 00:10:01,530
So at this point, you should pause the video and try your best to sell this program on your own.

125
00:10:04,060 --> 00:10:07,630
OK, so hopefully you pass the video and try that.

126
00:10:07,960 --> 00:10:12,010
I'm going to go ahead and go over a potential solution.

127
00:10:13,030 --> 00:10:14,350
So let me head here.

128
00:10:15,630 --> 00:10:18,900
So here's kind of what our old program looked like, right?

129
00:10:21,040 --> 00:10:30,310
We had just this loop right here that made the mold table, which was our 2D array, of course, I accidentally

130
00:10:30,310 --> 00:10:31,400
deleted it here.

131
00:10:31,420 --> 00:10:35,920
So we run it and it gives us this right here.

132
00:10:37,780 --> 00:10:41,830
So this is kind of what you should have had in the last program, but now we're going to change that

133
00:10:41,830 --> 00:10:43,150
into a dynamic array.

134
00:10:43,810 --> 00:10:49,780
So what we're going to do is start off with that syntax that we saw, right?

135
00:10:53,150 --> 00:10:56,850
So it's going to be in star star.

136
00:10:56,900 --> 00:11:03,560
I'm going to call it more table again and I'll say equals new and star.

137
00:11:05,000 --> 00:11:06,610
And it's not going to be three.

138
00:11:06,620 --> 00:11:08,680
We need to take that in from the user, right?

139
00:11:08,690 --> 00:11:13,580
So how will we get a table size?

140
00:11:14,660 --> 00:11:18,440
And we see in the table size.

141
00:11:19,490 --> 00:11:23,420
And then we're going to make this table size.

142
00:11:25,350 --> 00:11:30,390
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

143
00:11:30,390 --> 00:11:30,990
here, right?

144
00:11:31,000 --> 00:11:36,960
We need to make an array for each section of our initial array, right?

145
00:11:36,990 --> 00:11:41,070
Each position is going to be a pointer to another array of integers.

146
00:11:42,000 --> 00:11:50,370
So what I'm going to say is just go for and I equals zero.

147
00:11:50,370 --> 00:11:53,340
I is less than table size.

148
00:11:54,060 --> 00:11:56,100
I +, + +.

149
00:11:56,100 --> 00:12:02,430
I will go in here and I am actually going to do something like this.

150
00:12:02,430 --> 00:12:08,520
I will say Mult table, I equals new.

151
00:12:09,600 --> 00:12:17,130
So remember this D references our multi table and it's going to then leave us with an.

152
00:12:19,070 --> 00:12:27,710
A pointer that points to entered integers, which is going to be OK for us to assign an array to that

153
00:12:27,710 --> 00:12:30,710
of type integer, right, so we'll saying you int.

154
00:12:31,400 --> 00:12:37,040
And here we're just going to put our table size right because we want the rows and the columns to be

155
00:12:37,040 --> 00:12:37,730
the same.

156
00:12:40,770 --> 00:12:42,330
So we'll have kind of.

157
00:12:44,710 --> 00:12:48,640
That many rows and that many columns, right?

158
00:12:50,390 --> 00:12:54,230
So let's go ahead and continue on with this.

159
00:12:54,380 --> 00:12:54,890
So.

160
00:12:55,990 --> 00:13:01,570
What we need to change here is not have this be a three anymore, but instead it should be less than

161
00:13:01,570 --> 00:13:02,860
table size, right?

162
00:13:03,990 --> 00:13:07,140
And then here, we should say less than table size.

163
00:13:08,820 --> 00:13:11,310
This should be oops, I didn't mean to do that.

164
00:13:12,540 --> 00:13:22,110
There should be see, this should be table size on less than table size here and less than table size

165
00:13:22,110 --> 00:13:22,440
here.

166
00:13:23,720 --> 00:13:32,570
So we're still able to add things into our multiplication table because this basically the references

167
00:13:32,570 --> 00:13:35,600
because of these right here, the subscript operator.

168
00:13:35,990 --> 00:13:40,580
So this old reference that first pointer and then it will reference the second pointer into the correct

169
00:13:40,580 --> 00:13:43,790
position of the second dimension array.

170
00:13:44,420 --> 00:13:46,700
And then we just want to put the product there, correct?

171
00:13:47,690 --> 00:13:53,090
So this will do the same thing, you know, it'll actually do reference ID so we can print it out as

172
00:13:53,090 --> 00:13:53,420
well.

173
00:13:54,700 --> 00:13:58,330
So let's go ahead and save this, compile it and test it out.

174
00:14:00,660 --> 00:14:08,280
So I'm going to say it clear, I will compile it looks like it compiles, OK, let's run it and I'm

175
00:14:08,280 --> 00:14:10,830
going to go ahead and just put four here.

176
00:14:12,710 --> 00:14:19,670
And you notice that this table size of four gives us this extra dimension, so we basically have zero.

177
00:14:20,330 --> 00:14:26,660
It's basically zero one two and three zero one two and three.

178
00:14:27,170 --> 00:14:30,850
So at the end here we have three times three is nine.

179
00:14:32,000 --> 00:14:36,230
Two times, three, six, one times three is three.

180
00:14:37,210 --> 00:14:43,180
One times one is one, one times two is do all that good stuff, right, you don't use four to 10,

181
00:14:43,190 --> 00:14:43,870
three six.

182
00:14:44,620 --> 00:14:46,390
So pretty cool, right?

183
00:14:46,780 --> 00:14:48,760
And we can increase this size, though.

184
00:14:49,150 --> 00:14:50,630
So what if I was to go?

185
00:14:50,650 --> 00:14:56,860
Let me run it again, and I'd put like 12 or something like that.

186
00:14:58,090 --> 00:14:59,680
We'll call it print it out.

187
00:14:59,980 --> 00:15:06,190
You know, a lot of stuff, but this looks like kind of messy and out of place down here, right?

188
00:15:09,380 --> 00:15:11,780
So see how it's kind of offset.

189
00:15:13,170 --> 00:15:15,060
So you might notice that.

190
00:15:17,030 --> 00:15:19,040
You know, things aren't printing out so clean.

191
00:15:19,730 --> 00:15:28,040
And of course, I mean, you might be able to like add some more spaces in here, and you might think

192
00:15:28,040 --> 00:15:30,110
that that makes it a little bit nicer.

193
00:15:30,770 --> 00:15:35,720
Like if I was to do that and run it again with 12.

194
00:15:37,210 --> 00:15:42,310
Then it's like some stealth lines of a little bit better, but it's still is like pretty gross here,

195
00:15:42,460 --> 00:15:42,820
right?

196
00:15:44,670 --> 00:15:46,980
So what can we do about this?

197
00:15:47,010 --> 00:15:55,260
Well, I'm actually going to show you a way to align things a lot nicer in the next video.

198
00:15:56,270 --> 00:16:05,270
So we'll go over that, and we will also maybe go over another way to create two dimensional dynamic

199
00:16:05,270 --> 00:16:05,840
arrays.

200
00:16:07,310 --> 00:16:12,120
So hopefully this was educational for you.

201
00:16:12,680 --> 00:16:16,820
And if you didn't figure it out, always, of course, don't worry.

202
00:16:17,150 --> 00:16:22,430
Just go back, try and type this from memory now instead of just reading it.

203
00:16:22,730 --> 00:16:23,450
Take a look.

204
00:16:23,450 --> 00:16:30,980
And then each time you write a piece of code, try and stop and see if you understand what it is that

205
00:16:30,980 --> 00:16:33,810
you're writing and what it really means and why we use it.

206
00:16:33,860 --> 00:16:40,160
And if you need to go ahead and refer back to the lecture, you can look at C++ documentation.

207
00:16:41,380 --> 00:16:43,780
Or whatever you need to do.

208
00:16:44,650 --> 00:16:49,210
All right, so with that, I will go ahead and see you in the next lecture.
