OK, everybody.
One last while loop video for you.
This is actually just going to be a little practice problem for you where I will go through the solution,
but I think that we just need to go over one last little thing with loops in general.
Specifically, though, while loops for this time, and that is something like this where you need to
fill in array using a loop.
So this is a challenge for you.
This is something that we haven't really done yet, but I think that it is something that you might
be able to figure out.
But don't worry, I'll go over the solution.
So here is our problem statement.
It says Ask the user to fill an array with integers one position at a time once the array is full.
Go through that array and print out all the elements on the same line where the numbers are separated
by spaces.
So.
Here is the description.
I am going to let you go ahead and pause the video to see if you can figure this out.
And then I will go ahead and walk through it.
OK, so I'm going to go ahead.
I already have the code kind of written out here, but I'm going to go ahead and explain it.
So the first thing that we want to do is make an array.
I have hardcoded it here.
There's something interesting that I want to point out is that you can sometimes do something like this.
Let's say you were to get something from a user like a number and size and then you see in size.
It is possible to.
Put size here only on specific using a specific C++ compiler.
It's actually from.
The I think it was from the not C++, but C ninety nine or something like that allowed something like
this, but certain compilers, I think you might get an error if you do something like this.
So I'm going to put it like for this lecture, I'm going to put it just like hardcoded.
But you could go ahead and try and put safe here.
Do something like this.
If you want to get the size of the array from the user, you could do something like this.
But I don't think that this is a good programming practice because it might not work on certain C++
compilers.
You might get an error, as far as I know.
So I would say put this.
Five here, and we'll just kind of leave it like that in the next video, I might just go ahead and
use that size thing because it does work with my compiler, but it's not good programming practice.
To do that, we will talk about what we can do to make dynamic sized arrays rather than fixed size arrays
later on.
So then I have this interposition position as well because I need to fill an array with integers one
position at a time.
So what I'm going to do is make this little counter variable so I can know which position to put what
integer in the array.
Remember, an array is an indexed container, so we're able to place things at certain indices.
If you forget about that, go ahead and maybe rewatch the lectures where we have talked about race and
used to the floor.
So I also have this count right here.
This is something that I am defining for the second part, because I could use these same variable,
but I chose to use a different one to index it when I printed out.
So let's continue on to see what else I put here.
So let's take a look here at the loop.
So what we're going to do is loop as long as the position.
Is less than five, and why is that less than five?
See if you can answer that for yourself, the reason why is because the last item in nuns is actually
position four, because it starts at zero.
Remember really important things about race and pretty much most containers in C++ starting this zero.
So.
Then we asked the user inside the loop to enter a number for the position, and I print out the variable
here position.
What we do at that point is we read in directly into the array, so that's kind of cool, right?
We are able to read directly into the array right here.
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
into an array position before now instead of hard coding it like we're not putting a number here in
this highlighted section, we're putting the current position that we're on and when I say current.
That has to do with this next line, because what I'm doing is I'm actually increasing the position,
each iteration of the loop.
So each time we go through this while loop, I'm doing a plus plus position and I'm going to keep doing
that as long as position is less than five positions started out as zero, right?
I say, let's go as long as physicians are less than five and here is where we increase it from zero
up until it's five.
When position is five.
None of this code will run because the condition explicitly says that it will only execute this code
and here, as long as possession is less than five, you see in the less than sign here.
But the important thing is that the position is being incremented and it is also being used to fill
each next slot inside of our array.
Right.
So then we come to this next part, the next part said that we need to then go through the array and
print out all the elements on the same line where the numbers are separated by spaces.
So that is exactly what we do here.
We do a similar thing, I put another variable kind of just for visual purposes.
I could have reused position.
The way that I could have done that was to do something like position.
Oops.
Position equals zero.
So I reset position.
From being five to zero, because you know that what happens when this loop is done, position will
be equal to five.
And so it will come down here and do the code.
That's after this wildly.
And so at that point in time, position would be five.
So what we have to do is reset it to zero.
That's something we could do, is what I'm saying, but we don't need to because what I did was I just
made another variable called count, which is essentially the same thing as position is just going to
be used in this loop.
So I say, well, count less than five, then what I do is I see out.
The NUM's position count.
And then a space.
And so what happen, for example, is the first time it runs count will be zero.
So it'll print out numbers at position zero.
And then it'll put a sprint out of space.
It will then increment the count variable, so now count will go from zero to one.
It'll come back up here.
It'll be like is one less than five.
Yes, it is.
So then it will go here and it'll say, Let's print out NUM's position one.
And then a space go down here, count gets increased to two.
We go up here to is less than five.
We keep going and we keep going until count is five.
And then we're done and we return to zero and our program is finished.
OK, so that's really all there is to it.
If you weren't able to figure it out, don't worry.
Like I say in each lecture, it's not that big of a deal.
We're just trying to practice so you can feel more comfortable with these concepts.
So we will keep having more and more problems.
This is most likely going to be the last videos about while loops specifically, but we're going to
continue on with loops and later on in the course we're going to be using while loops more so you'll
get more practice with them.
All right.
So with that, I will see you in the next lecture, except in the next lecture, I think what I'm going
to do is I.
We're going to introduce another type of loop, and I may potentially use this kind of thing where I
read into a variable.
But I want to point out that.
Doing something like this for an array is generally not good practice.
They raise they somewhat seem they might somewhat limit us now as far as what we know about it, but
we are going to talk about a way to find a way to put a variable in here rather than just a number in
the near future.
All right.
So just take note of that, that it might work.
You might be able to do that, and I might just use it in the next lecture.
But it's bad practice to do that when you're making serious programs and stuff like that.
So I want to point that out.
OK, with that, I will see you in the next lecture.