OK, so in this lecture, we're going to go over some practice problems for the for loops.
So we introduce that just recently, but just like the while loops.
I think it's important that we kind of go over it a little bit more with some practice problems where
you can kind of try and solve things for yourself.
That way, it will really embed that knowledge.
So we only have two problems.
I think that you should be able to figure them out.
I will give a hint on one of them kind of in between just to help you out a little bit if it seems a
little confusing.
And then of course, after each, I will be going over the solution or a solution to them.
There's probably multiple solutions that you can do.
All right.
So let's go ahead and get started.
So problem one description says to make a program that takes in two integers from the user and then
prints out the result of the first number being raised to the power of the second number.
So that is where the second number is the exponent.
And you should do this by using a for loop.
So you see an example here, so basically what it's doing is in this first example, you ask the user
for a whole number.
The user in this example puts two and then afterwards you ask them for another number.
And the user puts five.
And what it does is it calculates two to the fifth power.
So.
You know, two to the five, and it says that equals three to.
It again, say intro hole, no.
This is two and then seven is the second number, which is the exponent and says two to the seven two
one two eight.
We noticed the same thing down here to the 12 is 496.
And these are significant numbers in computer science.
Of course, your program should take in any numbers that are integers.
And print out the integer result, but powers of two are significant in computer science because of
binary, and when you're thinking about bits and bytes and stuff like that, you know, there there
are definitely significant numbers.
Quite often you will need to count and powers of two and know the results kind of at the top of your
head, like knowing what to to the fifth is is pretty good when you're talking about 32 bit and 64 bit
machines and address bases and architectures and things like that.
So definitely useful.
That's why I put two to this something as an example for these three examples.
OK.
So my challenge for you is to pause the video and try and solve this on your own.
And I will go ahead and go over my solution.
There's multiple ways to do it.
And so if you didn't, don't necessarily do it the way that that I did, that's totally fine.
But the important part is to use a for loop.
So go ahead and pass the video and try and do it right now.
I will give a another hint and let you kind of try it again and then I will show you the answer.
OK, so a slight hint that might help you if you are struggling is the for loop can be used to basically.
Calculate the result.
By using the idea that something to a certain power is basically the first number, of course, multiplied
this many times.
As the second number, so it's basically, you know, two to the fifth is two times, two times, two
times two times two.
Right?
Kind of like an easier example is like two to the third power is eight, which is two times two times
two.
Which, you know, that's like two times two is four, four times two is eight something like that.
So you can use the concept of the loop to basically make this multiplication happen a specific number
of times and then also know that you're going to have to.
Kind of continuously multiply and keep track of a result, right, because you did two times, two is
four.
Now you have the number four and now you have to do.
Times two again.
So you got to take your results so far, which is four.
And then you multiply that times two again, and that's how you get eight.
And let's say you and I have two to the fourth power, you would now want to take that eight and multiply
it times two again.
The fourth loop is used to just do repetition, right?
So you got to think of how to keep track of like a total right for your result.
And then you need to multiply continuously by whatever the first number is.
So that is like my big hint, so if you haven't been able to figure it out yet, go ahead and pass the
video now and try again.
OK, and so now I'm going to exit out of here and go to Visual Studio to go over a result or a solution
to this.
So let me see if I can increase the text size a little bit of this.
This is big enough for you.
Let's see.
Yeah, so I think that's I think that's good enough.
So I just had it already typed out.
Unfortunately, I have a wrist injury right now, so I can't type very fast, and it would be kind of
annoying to watch me chicken peck the keyboard and I really slow.
So I just wanted to type it out previously so you can go over it so you don't have to watch my painfully
slow typing process with one hand.
All right.
So what I did, of course, was start how we always do so far, which is a main function and of course,
including extreme stream.
At the top here.
And I'm using namespace as CD.
You don't have to use that, of course, you know how that goes.
So we declare a first integer, right?
We're going to need two integers, so we should declare those.
So I declare these two integers.
I also go ahead and declare a result that I'm going to keep track of.
To print out right off the bat.
And I don't initialize it yet, but this is totally up to you.
You could do this different when it's you don't have to define it first or sorry, declare first and
then initialize it later, but.
That's how I did it.
So first, I asked the user here to enter a whole number, right?
And then what I do is I read that number into the first integer I want.
Then we asked them to enter an exponent.
And then we read that explanation, and then what I do is the way that I did it and there's multiple
ways you can do this, but I said, OK, well, when you start out in the beginning, basically you
have if you're thinking of the idea of let's let's take two to the three for an example, as a simple
example to the third power you start out with two, right?
And then what you do is you go, you know, two times, two times two.
Right.
So you start out with two.
And then you do times to once in time times do twice.
Right.
So that's kind of what I thought about is that, OK, I'm going to start out with two, for example.
Start out with two.
And then what I was going to do is have a loop that doesn't start at zero, but it starts out at one
because two to the first power is one.
In fact, any number to the first power is that number itself.
Right?
So that's kind of what I started out with as I said, OK, I'm going to start my eye out at one.
And then what I'm going to do is I'm going to go until it's less than.
The second number that was entered, so let's take two to three, for example, so I'm going to start
at one, so we have two to the one and then one I need to do.
Is I basically need to do.
A times two twice, right?
I don't want to do it three times, so that's why I'm doing less than I to.
So I two is three.
And I want to do it twice when it enters the falou.
It's going to multiply it.
This for this first time when I equals one, right, and then it's going to go again and multiply it
again when I equals two.
I only want the multiplication to happen two times, right?
Because I only it's already the result is already too.
So you notice right here I have a times equal.
So this would be the same thing as saying result equals resort times.
I won.
I.
Result equals resort times I want that is basically the same as this, but this is a simplified version
of it using the times equal.
Operator.
So what's going on is that let's take this two to three example here.
So result equals two, let's just say the user entered to as the first number.
And three, as the second number, so we start out with a result is to right.
We start out with I equals one and we say go from eight is one until I is as long as I is less than
three.
Right, so we go in here and we say, OK, resort is too.
Now we're going to say resort is now two times two.
So that's four, right?
So the first time we run this line, we're doing result equals resort times to.
So the first number was two, and we're saying result is now two times two, so it's four.
I was one.
We go back up.
Now I is two.
It's not one.
It's two.
So he is I is too sorry to less than three.
So I is to now and we say, is I less than three?
It is right, so we still go again.
So remember, our result is four, that's what we did last time.
So now we're seeing a result actually equals four times to.
So four times two is eight.
Right?
So now we store eight in resort and then we go back up and now is going to be three.
And we say is three less than three.
No three is not less than three, it's equal to three.
So we stop.
Right.
And then we go down here, you print out what our first number was raised to the second no power.
And then we say and this, you know, this is just printing that out is basically saying like, you
know, first no, and then using the carrot symbol and then the second number that they entered to just
show what the result is that we say equal result is eight.
It has the correct result for a two to the third power and then we do it in line.
So.
I think that I am going to.
Go ahead and try and tape this so I can run it again.
OK, so two and then three, OK, we noticed that it's eight, right?
Just to show you that I wasn't messing with you.
That's really what they were sort of two of their powers now on to walk through how I did it.
Another way that you could have done this.
Actually, there's a there's probably multiple ways like a decent amount of ways, a handful of ways,
but you can also think of it as starting at zero.
If you wanted to, you could say start with I equals zero and I'm going to start at Russell equals one
because two.
Sorry, I can't just to any number to the zero power is going to be one, right?
So that's just a mathematical concept.
And if we wanted, we could actually start at zero if we want to with a loop.
But then what we should do is we should say, OK, if we're talking about a zero exponent, we should
be then considering to restart our result at one because let's take two, for example, again two to
the three power again.
Two to the zero is going to be one.
Right, so if we're going to start that, we want to make sure that it's actually starting at one and
then if we're going to go again, then we can basically do, you know, one times two, and that would
bring us to two to the one.
And then, you know, times two again would bring us two squared, which is four, right?
So I can actually save this.
I will try to compile this.
All right.
So let's try and do this again just to show that it works.
So I do, too.
I'm with you three and we get the same result here.
Right.
Cool.
So that is the solution to the first problem you could do either of these two ways or you could do.
Maybe you did differently and that's great.
As long as it works in good job, I'm glad that you were able to figure it out if you couldn't figure
it out.
Don't worry.
Keep practicing.
It's just a matter of drilling this until you get it down and understand how the for loop works better.
OK, so let's go ahead and head back to our presentation here.
So now we're going to move on to the second problem.
So this second problem?
A pretty simple description.
But it says to make a program that takes a word and this word is going to be of the data type string.
So you're going to make a string.
But it specifically says word because we're not talking about a sentence like we're not talking about
multiple words separated by spaces.
In the example, here you notice that it's just single words.
And what you do is you take that word from the user, which is a string and print that word in reverse.
So this one is kind of a much more simple idea.
Not having to do as much work.
But it may be a little confusing because we haven't really looked at an example that does this style
of thing with a photo loop, I have mentioned it.
But we haven't really done something like this yet.
So just like the last one, I will encourage you to solve it on your own without me giving any hints.
Then I will give a hint and you can try it again if you haven't solved it.
And then if you haven't solved it by then, you can look at the solution that I will go over.
OK.
So, yeah, it's pretty simple this, isn't it.
A couple of examples here.
So in this example, the user entered A-word programming and then it prints out reversed word equals
and then it shows the prints out the word in reverse here.
Right.
So note that I am not.
Saying that you need to reverse the string so you don't necessarily have to reverse the string and then
print the string out, although you can do that, if you would like to do that as a challenge, you
can definitely do that.
But all I'm really saying is that you need to print out the word in reverse, right?
So just kind of remember that if you want to reverse the string, you can go ahead, but you don't have
to do that.
You can just print the word in reverse.
So you notice.
Yeah.
Here's one example.
Here's another example of software so pretty straightforward.
All right, so go ahead and of video.
I encourage you to try and solve it right now without any hints or help.
OK, so if you weren't able to solve it, I'm going to give a hint now.
So something that you can do for this to print it out in reverse is, of course you should be using
a for loop.
But.
What you can do is instead of looping forward with incrementing with the A-plus plus, you can actually
loop backwards with decremental with the decrement operator, which is I minus minus.
So rather than starting at zero and going up to the bike length of the string minus one, you can actually
reverse that.
You can actually start at the end of the string, which is the length of the string minus one, and
then you can go backwards until you reach the zero position.
All right, so that's a hint, so if you have, you weren't able to solve it.
In the beginning, go ahead and try that video and try to solve it again, and then I will go over the
solution.
OK, so now I'm going to go over the solution or at least one of the potential solutions.
So I have these kind of flipped this says practice to but, you know, it should have.
It should have said practice one.
But this is what we're doing now.
So once again, we, of course, start out with our basic stuff here.
And you may or may not need to include a string.
You might need to include string up here as well.
In addition to that, maybe it's dependent on C++ version.
I'm not a hundred percent sure.
Actually, I didn't need to use it because of whatever version I'm using, but I know that string gives
you access to some of these member functions that you can call on the string.
So that's why you if it's not working, if it if you get an error where it doesn't understand a dot
length.
You're having a problem with that, then you might need to go up here and do a hashtag include string
as well.
Otherwise, don't worry about it, so we have our main function, right?
And then I declare a variable to read into right.
I'm going to make this variable here.
I called it word and then we print out to the user that they should enter a word.
Then we read into that word we're seeing.
Then what we do is we put we print out reverse word equals because we're about to print out the result,
right?
So we're going to tell the user, OK, I'm about to print out the result because afterwards we're going
to put the actual result.
And this is how I went about it.
So I did it for loop and I said, and I is equal to you, word length minus one.
So I save where dot length minus one into I.
This is the last position of the string, right?
We take the size of the string.
How long is it?
And then since the indexing starts at zero, we need to do that minus one, right?
That'll be the last position of the string.
So.
If I entered programming, it would be at the last G.
Right?
So then what we say is how long are we going to loop for, what is this condition based on?
Well, we're going to loop as long as I is greater than.
Or equal to zero.
And we put in or equal to.
Because we still want to print out the zeroth character, right, these zero position character, so
programming as an example, again, the programming word P would be the first character.
That's it positions zero.
Right?
So we started G, which is the last character of programming, and we're going to want to print that
out and then we're going to want to keep going as long as it's greater than or equal to zero.
So we will keep subtracting and we'll print out the word until we get to the P and we'll go ahead and
print out the very first character, which is P, that's in the zero position.
And then we're done right because it'll go into the negatives after that.
So we use a decrement operator because we're going backwards.
We start at the end of the word, the last character we go up and tell the first character.
So it's kind of more like down.
I guess we're going down until the first character.
And we're doing that backwards subtraction, subtraction of the position, which is I by doing a minus
minus, this is as decrement operator.
And so each time we're printing out whatever position we're on, right?
So let's take a very simple word for, except for an example, so how about the word car?
So it's c.a.r..
So the user enters the word car.
Actually, what we do first is we say, hey, enter a word, please.
They enter a car that gets red into this string variable word.
So now the string variable where it holds the word car.
Then we say, OK, reverse word equals we print that out.
And now we're going to go and try and calculate the result and print it out.
So we start this for loop.
I is going to be equal to word length minus one.
What is word length if it's car?
Well, it's three characters, long, right, c.a.r. one to three.
So three characters long.
But then we say minus one.
So instead of three, it's going to be two.
And why is that?
Because I'm going to try and type really badly here at risk.
But see, is it position zero, right?
A Is it position one, right?
Ah, is it position two?
So we did a three minus one is two.
This is a position two.
This is where we want to start is on the R.
It's a position two.
We want to go until position zero, which is the sea, right?
So then we say, OK, is I greater than or equal to zero?
Yes, two is greater than or equal to zero.
So we're going to go down into here.
And we're going to start out on.
And so what we're going to do is we're going to print out.
What is that word position to?
Well, ah, is it we're in position to right, we have two zero one two position, two is ah.
So we print out our.
We go back to the top of the loop.
We do a minus minus, right, so now we're at position one, which is this a we basically say is one
greater than or equal to zero?
Yes, it is.
So we go in here and we say print out word position one.
We're in position one is here, right?
This is weird, position one is this middle one right here because this is two, this is one, this
is zero.
So we point out that a so now we've printed out in our first this is what's printing out are and now
in a.
And then what we do is we go back to the top and now we subtract one from one, and that gives us zero.
And we say OK is zero greater than or equal to zero zero is not greater then, but it is equal to so
greater or equal to or greater than or equal to operator.
So it is equal to so this this condition is still satisfied.
We still go down in here.
We say, let's print out word at position zero.
We're in position zero, is this character right here, see, right?
We were here at one, we subtracted one and went left one.
And now we're at position zero, which is, see, so we print out.
See?
All right.
So we print out this.
And in fact, there's no spaces, I just put the spaces here, so you could visibly see it better as
I was showing you the positions.
But the result is going to be like this, right?
So let's go ahead and prove this, and then I print out a little in line just for accurate kind of.
So I'm going to save, I'm actually just going to stay right here so you can see that I clicked and
saved and I am going to do this is going to be hard to Typekit C++ f l crack test one
o f l p one c so i compile that.
OK, and I'm going to run this into a word, and I'm going to tape car and I press enter and you notice
that it reverses it and does this are a see just like we expected.
OK, so hopefully that wasn't too crazy, difficult it might have been because.
You haven't haven't really showed you an example using the minus minus, but really important to know
that you're not limited to just doing this.
I and II equals zero.
I less than something I.
Plus plus you don't have to do that all the time.
That's not what a for loop is.
A for loop is really open to interpretation.
You can put whatever for this value and you can put whatever condition and you can put like a bunch
of different operators here.
For the incrementing and decorating and stuff.
So just know that you could put like, you know, word lengths like plus 10, you could just put like
the constant one, you can put the number one thousand here, you can put some other variable here.
Right.
This could be like equals equals.
This could be less than or equal to.
This can be greater than an equal to.
It could be less than it could be greater than it could be like not equal to.
You can put all kinds of stuff here and then here, you know, this could be like.
All kinds of different things as far as how much you're incriminating, I.
Each time or not, you maybe you're decorating by one, maybe you're incriminating by 10, you know?
Multiplying by two, you can do you do doing all kinds of stuff here.
So it's important to understand that you can customize these four loops to move forward and backward
through some sort of string or an array or a for loop doesn't have to be just going through an array.
You notice that it can be used for counting and stuff like that, too, as long as you know a specific
number or the user's going to enter something that can be translated to a number of times that a loop
can run.
That's when you should use a for.
Right.
In some point in your program, if you're going to be able to extract some value or know some value
or use some value as a.
Can be used as a number of times for a loop to iterate.
Then you can build a for loop with that in mind here.
And once again, you don't have to use AI.
You can use whatever variable makes sense.
It's commonplace to use AI a lot of times if you just have one Lou.
It's just kind of like a programming thing that kind of caught on, you know, like an iteration or
something like that, but you don't have to you.
OK?
So hopefully this helped you understand loops a little bit better.
Of course, looking at more problems online and also coming up with your own practice problems is great
as well.
But you don't have to worry too much because we're going to be using for loops a lot.
You're going to notice that a ton of problems that we solve are going to be using for loops and while
loops as well.
But a lot, a lot of loops, definitely a lot of for loops.
They're huge for algorithms, and we're going to be doing a lot of algorithms in the second part of
the course, even in the this first section, of course, as well.
OK, so with that, I will see you in the next lecture where we will start getting into some new stuff,
we're going to leave loops kind of behind.
We'll still keep using them, but we're not going to explicitly be focusing on them.
We're going to introduce some new concepts and try and use the loops with those new concepts as well.
All right.
So with that, I'll see you in the next lecture.