1
00:00:00,600 --> 00:00:01,560
OK, everybody.

2
00:00:01,980 --> 00:00:04,320
One last while loop video for you.

3
00:00:05,310 --> 00:00:13,160
This is actually just going to be a little practice problem for you where I will go through the solution,

4
00:00:14,070 --> 00:00:19,140
but I think that we just need to go over one last little thing with loops in general.

5
00:00:19,440 --> 00:00:26,340
Specifically, though, while loops for this time, and that is something like this where you need to

6
00:00:26,370 --> 00:00:29,550
fill in array using a loop.

7
00:00:29,640 --> 00:00:32,340
So this is a challenge for you.

8
00:00:32,370 --> 00:00:39,600
This is something that we haven't really done yet, but I think that it is something that you might

9
00:00:39,600 --> 00:00:40,860
be able to figure out.

10
00:00:40,860 --> 00:00:42,360
But don't worry, I'll go over the solution.

11
00:00:42,360 --> 00:00:44,100
So here is our problem statement.

12
00:00:44,670 --> 00:00:53,640
It says Ask the user to fill an array with integers one position at a time once the array is full.

13
00:00:53,760 --> 00:01:00,180
Go through that array and print out all the elements on the same line where the numbers are separated

14
00:01:00,180 --> 00:01:00,990
by spaces.

15
00:01:02,480 --> 00:01:02,990
So.

16
00:01:04,060 --> 00:01:05,440
Here is the description.

17
00:01:06,830 --> 00:01:12,380
I am going to let you go ahead and pause the video to see if you can figure this out.

18
00:01:12,800 --> 00:01:15,440
And then I will go ahead and walk through it.

19
00:01:18,060 --> 00:01:19,810
OK, so I'm going to go ahead.

20
00:01:19,830 --> 00:01:24,600
I already have the code kind of written out here, but I'm going to go ahead and explain it.

21
00:01:26,060 --> 00:01:32,060
So the first thing that we want to do is make an array.

22
00:01:33,050 --> 00:01:34,910
I have hardcoded it here.

23
00:01:37,730 --> 00:01:48,650
There's something interesting that I want to point out is that you can sometimes do something like this.

24
00:01:49,130 --> 00:02:00,740
Let's say you were to get something from a user like a number and size and then you see in size.

25
00:02:04,100 --> 00:02:06,770
It is possible to.

26
00:02:09,700 --> 00:02:16,250
Put size here only on specific using a specific C++ compiler.

27
00:02:16,750 --> 00:02:18,430
It's actually from.

28
00:02:20,320 --> 00:02:29,650
The I think it was from the not C++, but C ninety nine or something like that allowed something like

29
00:02:29,650 --> 00:02:36,370
this, but certain compilers, I think you might get an error if you do something like this.

30
00:02:36,370 --> 00:02:42,670
So I'm going to put it like for this lecture, I'm going to put it just like hardcoded.

31
00:02:43,930 --> 00:02:46,840
But you could go ahead and try and put safe here.

32
00:02:47,680 --> 00:02:48,640
Do something like this.

33
00:02:48,640 --> 00:02:53,770
If you want to get the size of the array from the user, you could do something like this.

34
00:02:53,770 --> 00:03:00,610
But I don't think that this is a good programming practice because it might not work on certain C++

35
00:03:00,910 --> 00:03:01,510
compilers.

36
00:03:01,510 --> 00:03:04,060
You might get an error, as far as I know.

37
00:03:05,700 --> 00:03:07,170
So I would say put this.

38
00:03:08,260 --> 00:03:17,770
Five here, and we'll just kind of leave it like that in the next video, I might just go ahead and

39
00:03:17,770 --> 00:03:24,580
use that size thing because it does work with my compiler, but it's not good programming practice.

40
00:03:24,580 --> 00:03:31,930
To do that, we will talk about what we can do to make dynamic sized arrays rather than fixed size arrays

41
00:03:31,930 --> 00:03:32,590
later on.

42
00:03:34,030 --> 00:03:42,400
So then I have this interposition position as well because I need to fill an array with integers one

43
00:03:42,400 --> 00:03:44,130
position at a time.

44
00:03:44,140 --> 00:03:53,410
So what I'm going to do is make this little counter variable so I can know which position to put what

45
00:03:53,620 --> 00:03:55,050
integer in the array.

46
00:03:55,060 --> 00:04:03,320
Remember, an array is an indexed container, so we're able to place things at certain indices.

47
00:04:03,430 --> 00:04:10,240
If you forget about that, go ahead and maybe rewatch the lectures where we have talked about race and

48
00:04:10,240 --> 00:04:12,880
used to the floor.

49
00:04:13,390 --> 00:04:16,070
So I also have this count right here.

50
00:04:16,090 --> 00:04:24,160
This is something that I am defining for the second part, because I could use these same variable,

51
00:04:24,700 --> 00:04:28,600
but I chose to use a different one to index it when I printed out.

52
00:04:29,170 --> 00:04:32,230
So let's continue on to see what else I put here.

53
00:04:32,440 --> 00:04:36,070
So let's take a look here at the loop.

54
00:04:36,220 --> 00:04:41,050
So what we're going to do is loop as long as the position.

55
00:04:42,450 --> 00:04:46,860
Is less than five, and why is that less than five?

56
00:04:47,550 --> 00:04:55,200
See if you can answer that for yourself, the reason why is because the last item in nuns is actually

57
00:04:55,200 --> 00:04:58,260
position four, because it starts at zero.

58
00:04:58,270 --> 00:05:06,180
Remember really important things about race and pretty much most containers in C++ starting this zero.

59
00:05:08,960 --> 00:05:09,470
So.

60
00:05:10,830 --> 00:05:20,520
Then we asked the user inside the loop to enter a number for the position, and I print out the variable

61
00:05:20,520 --> 00:05:21,660
here position.

62
00:05:23,870 --> 00:05:30,860
What we do at that point is we read in directly into the array, so that's kind of cool, right?

63
00:05:31,860 --> 00:05:34,890
We are able to read directly into the array right here.

64
00:05:36,480 --> 00:05:42,060
So how we're doing that as we're doing a scene, we use the name of the array and just like we've read

65
00:05:42,060 --> 00:05:48,510
into an array position before now instead of hard coding it like we're not putting a number here in

66
00:05:48,510 --> 00:05:54,120
this highlighted section, we're putting the current position that we're on and when I say current.

67
00:05:55,430 --> 00:06:01,190
That has to do with this next line, because what I'm doing is I'm actually increasing the position,

68
00:06:01,730 --> 00:06:04,310
each iteration of the loop.

69
00:06:05,360 --> 00:06:12,290
So each time we go through this while loop, I'm doing a plus plus position and I'm going to keep doing

70
00:06:12,290 --> 00:06:19,700
that as long as position is less than five positions started out as zero, right?

71
00:06:21,640 --> 00:06:28,000
I say, let's go as long as physicians are less than five and here is where we increase it from zero

72
00:06:28,330 --> 00:06:29,590
up until it's five.

73
00:06:31,400 --> 00:06:33,290
When position is five.

74
00:06:34,320 --> 00:06:40,560
None of this code will run because the condition explicitly says that it will only execute this code

75
00:06:40,560 --> 00:06:44,760
and here, as long as possession is less than five, you see in the less than sign here.

76
00:06:45,750 --> 00:06:52,470
But the important thing is that the position is being incremented and it is also being used to fill

77
00:06:52,470 --> 00:06:55,500
each next slot inside of our array.

78
00:06:55,690 --> 00:06:56,000
Right.

79
00:06:58,220 --> 00:07:03,800
So then we come to this next part, the next part said that we need to then go through the array and

80
00:07:03,800 --> 00:07:08,870
print out all the elements on the same line where the numbers are separated by spaces.

81
00:07:09,990 --> 00:07:12,180
So that is exactly what we do here.

82
00:07:12,510 --> 00:07:17,970
We do a similar thing, I put another variable kind of just for visual purposes.

83
00:07:18,090 --> 00:07:20,180
I could have reused position.

84
00:07:20,190 --> 00:07:25,060
The way that I could have done that was to do something like position.

85
00:07:25,620 --> 00:07:26,020
Oops.

86
00:07:27,210 --> 00:07:28,560
Position equals zero.

87
00:07:28,560 --> 00:07:30,780
So I reset position.

88
00:07:31,720 --> 00:07:37,450
From being five to zero, because you know that what happens when this loop is done, position will

89
00:07:37,450 --> 00:07:38,560
be equal to five.

90
00:07:39,010 --> 00:07:42,340
And so it will come down here and do the code.

91
00:07:42,340 --> 00:07:43,960
That's after this wildly.

92
00:07:44,710 --> 00:07:46,720
And so at that point in time, position would be five.

93
00:07:46,750 --> 00:07:48,820
So what we have to do is reset it to zero.

94
00:07:48,820 --> 00:07:53,860
That's something we could do, is what I'm saying, but we don't need to because what I did was I just

95
00:07:53,860 --> 00:07:59,020
made another variable called count, which is essentially the same thing as position is just going to

96
00:07:59,020 --> 00:08:00,160
be used in this loop.

97
00:08:01,790 --> 00:08:06,020
So I say, well, count less than five, then what I do is I see out.

98
00:08:07,500 --> 00:08:11,130
The NUM's position count.

99
00:08:12,320 --> 00:08:13,460
And then a space.

100
00:08:14,180 --> 00:08:18,980
And so what happen, for example, is the first time it runs count will be zero.

101
00:08:19,430 --> 00:08:22,700
So it'll print out numbers at position zero.

102
00:08:25,000 --> 00:08:26,860
And then it'll put a sprint out of space.

103
00:08:27,310 --> 00:08:31,650
It will then increment the count variable, so now count will go from zero to one.

104
00:08:31,660 --> 00:08:33,250
It'll come back up here.

105
00:08:33,670 --> 00:08:35,590
It'll be like is one less than five.

106
00:08:35,620 --> 00:08:36,520
Yes, it is.

107
00:08:37,120 --> 00:08:41,800
So then it will go here and it'll say, Let's print out NUM's position one.

108
00:08:42,810 --> 00:08:46,780
And then a space go down here, count gets increased to two.

109
00:08:46,800 --> 00:08:48,960
We go up here to is less than five.

110
00:08:49,530 --> 00:08:53,580
We keep going and we keep going until count is five.

111
00:08:53,580 --> 00:08:57,930
And then we're done and we return to zero and our program is finished.

112
00:09:00,060 --> 00:09:03,750
OK, so that's really all there is to it.

113
00:09:04,260 --> 00:09:07,560
If you weren't able to figure it out, don't worry.

114
00:09:07,680 --> 00:09:11,340
Like I say in each lecture, it's not that big of a deal.

115
00:09:11,560 --> 00:09:16,350
We're just trying to practice so you can feel more comfortable with these concepts.

116
00:09:16,620 --> 00:09:18,960
So we will keep having more and more problems.

117
00:09:19,860 --> 00:09:26,700
This is most likely going to be the last videos about while loops specifically, but we're going to

118
00:09:26,700 --> 00:09:33,210
continue on with loops and later on in the course we're going to be using while loops more so you'll

119
00:09:33,210 --> 00:09:34,500
get more practice with them.

120
00:09:36,030 --> 00:09:36,510
All right.

121
00:09:36,660 --> 00:09:43,920
So with that, I will see you in the next lecture, except in the next lecture, I think what I'm going

122
00:09:43,920 --> 00:09:45,420
to do is I.

123
00:09:46,980 --> 00:09:53,760
We're going to introduce another type of loop, and I may potentially use this kind of thing where I

124
00:09:53,760 --> 00:09:57,540
read into a variable.

125
00:09:57,570 --> 00:09:59,640
But I want to point out that.

126
00:10:00,680 --> 00:10:05,810
Doing something like this for an array is generally not good practice.

127
00:10:06,440 --> 00:10:13,640
They raise they somewhat seem they might somewhat limit us now as far as what we know about it, but

128
00:10:13,640 --> 00:10:24,080
we are going to talk about a way to find a way to put a variable in here rather than just a number in

129
00:10:24,080 --> 00:10:24,920
the near future.

130
00:10:26,120 --> 00:10:26,420
All right.

131
00:10:26,450 --> 00:10:28,520
So just take note of that, that it might work.

132
00:10:28,970 --> 00:10:32,540
You might be able to do that, and I might just use it in the next lecture.

133
00:10:32,540 --> 00:10:38,720
But it's bad practice to do that when you're making serious programs and stuff like that.

134
00:10:38,720 --> 00:10:40,550
So I want to point that out.

135
00:10:40,730 --> 00:10:42,860
OK, with that, I will see you in the next lecture.
