Okay so welcome to the section 8 challenge solution.
I hope you were able to solve this problem,
and I hope it was fun to do. And I hope you tested it and
tweaked it, made it your own,added some features to it and so forth.
Let me just go through a couple of solutions. First, we'll do the solution
without using the modulo operator.
So I'm going to scroll up here, and let me just explain what I've got going on. Here's
my main, right at the beginning.
Okay. So I've defined conversion constants. These are just simple integers,
and it's the value in cents of a dollar, a quarter, a dime and a nickel.
Okay. So a dollar is 100 cents, a quarter is 25 cents and so forth.
Then I've got another integer called change amount.
This is what I'm asking the user to enter.
All right. So let's suppose that we're entering 92 here,
and we'll walk through a simple example.
Here, I'm declaring all the variables that I'm going to need:
dollars, quarters, dimes, nickels and pennies are going to contain
the number of dollars, quarters, dimes, nickels and pennies that I need to provide the change.
And then I'm going to have another variable called balance,
and that's going to be a balance, a running balance. So every time that I figure out
I need two dollars and I'm going to subtract two dollars from the original amount
until I get down to zero.
Okay. So that's my algorithm or my solution
is basically to determine how much of the highest currency I need
and then subtract that and have a running balance until it becomes zero.
All right. So let's do this example here.
And I'm just going to write some letters over here. I'll write dollars,
quarters, dimes, nickels and pennies.
So we can keep it running.
And I'll have a balance variable here as well.
All right. So let's start. We're right over here on line 57,
and we need to figure out how many dollars
are in 92 cents. Well, there are none, right.
So what we want to do is we want to divide the change amount which in this case is 92
divide that by the dollar value which is a 100 cents per dollar.
In this case, that's going to give me a 0.
Remember, we're using integer division here. So I'm going to get to 0,
which says no dollars.
Makes sense, right because I've got less than 100 cents.
Now I need to update my balance.
Well, how do I do that? I decrement however many dollars times a 100.
Well, I had 0 here, right. So in this case, the change amount is going to be 92
and I'm going to subtract from that
0 times 100.
Okay. So in this case, I'm not subtracting anything. My balance becomes 92.
Okay. So that's the first step.
Now we need to determine how many quarters we need.
So I'm going to take that balance, which is 92.
And I'm going to divide it by the quarter's value which is 25 cents.
And that will give me a 3,
right. 75 cents so I need 3 quarters.
Now what I want to do is I want to subtract that 75 cents from the 92.
And in this case, I'm using a compound operator right here, the minus equals.
So all I'm saying is balance equals balance minus the right-hand side.
Well, the right-hand side is 3 quarters
times 25 cents each, which is the 75.
Okay. So I'm going to subtract 75 from the balance,
which is going to give me 17. So that's my current balance.
Now I can work on the dimes. How many dimes?
Well, it's the balance which is 17 divided by 10 cents per dime.
So this is going to give me 1. So that's 1 dime.
And I'm going to do the same thing here. Hopefully, you can see what's going on.
I'm going to subtract 1 dime which is 10 cents each from the balance.
So I'm going to subtract 10 from the balance, gives me 7.
Now I'm going to figure out how many nickels I need,
7 divided by 5 cents per nickel. So I need 1 nickel.
I subtract 5 from the 7, and I end up with 2.
My balance is the number of pennies I need.
I don't need to do any more calculations, and that gives me the 2 pennies,
and we're basically done. So if we add up all of this, let's test this out,
this is going to give me 0, this is going to give me 25
times 3 which is 75 cents,
this will give me the 75, this will give me 10,
1 times 10, 1 times 5, and 2 times 1.
If I add all those guys up, I should have 75, 85, 92,
exactly what I expected.
Okay. So that gives you an example of one way to approach this.
We'll do it really quickly with another example here.
Let's say we wanted to use something like 267.
So let me clear this, and I'll go through this one really quickly this time.
So let's say it's 267 is the amount that the user entered.
Okay. So here, how many dollars? Well, 267
divided by 100, right. 100 cents
dollar that's going to give me 2 dollars.
Then what I want to do is I want to subtract, I'll run my balance up here
those 200
which gives me 67 cents. That's my balance.
How many quarters in 67 cents,
divide that by 25, that gives me 2 quarters,
which is 50 cents. I'm subtracting 50 cents,
and we're right back to where we were with the other problem where we've got 17.
How many dimes divide that by 10, it gives me 1 dime,
that's 10 cents. I subtract 10 from that. I end up with 7.
How many nickels, divide that by five. It gives me 1 nickel.
And I've got 2 left, which is my pennies,
right. So this gives us
right here 100 times 2, it gives us the 200.
This gives me 50. This gives me 10, 5 and 2.
Add all that up, I get 250, 260, 270.
I'm sorry 200, 250, 267,
which is exactly what we started out with.
Okay now we can take advantage of something here.
If you notice if I have 267
and I divide that by 100, let's say, that gives me 2 dollars integer division.
But notice that I have a remainder.
My remainder is 67, right.
That's where we can use the modulo operator to rework this a little bit.
So we'll do that next.
Okay. So let's look at another solution. This time we'll be using the modulo operator.
So suppose the user enters 267
for the change amount.
And I've put all these on the same file. That way you can
play around with both of them a lot easier.
So in this case, I'm zeroing everything out just like when we started.
So I've got 267 cents, that's what the user has entered,
and we'll just keep a running total over here for the dollars,
the quarters, the dimes, the nickels and the pennies.
Okay. So how many dollars? Well, we divide 267 by
100 cents per dollar, and that gives us the 2 dollars.
So we definitely need that. Now rather than subtract
the 2 times 100 from the 267 as we did before,
we can take advantage of the modulo operator.
So in this case, the change amount is 267
mod 100, right.
This gives us 2 with a remainder of 67.
So the effect is exactly like
subtracting 200 from 267.
Okay. So what we can do is we'll keep our balance down here this time just to have a little bit more room.
In this case, our balance is going to be 67.
That's what we just assigned right here.
Now let's figure out how many quarters we have. Well, our balance is 67,
divide that by 25 cents per quarter and that gives us 2 quarters.
So now again instead of subtracting 50 from 67,
we could just use the modular operator. Here, I'm using the compound operator.
So it's balance equals balance mod quarter value.
Same idea. So balance is 67
mod 25,
gives us 2 quarters right. That's 50
with a remainder of 17. So our balance becomes 17.
And then from this point forward, I think you could see what's happening.
Our balance is now 17
divided by 10 cents per dime, gives us 1 dime.
And then we do the same modular operator. So we're going to have 17
mod 10,
gives us 1 with a remainder of 7.
That's really what we care about as a remainder in this case.
How many nickels,
7 divided by 5 cents per nickel,
it gives us 1 nickel.
And then we do the mod operator one last time, we're going to say
the balance would be 7,
which is my balance, mod 5 which gives me a 1 with a remainder of 2.
At that point, our balance is 2 which is the number of pennies that are left.
And again if we add this up, we're going to have 2 dollars here, which is 200.
We've got 50 cents here, 10 cents here,
5 cents here and 2 cents here,
which gives us 200, 250, 267.
okay. Another approach to me this approach -- I'll clear this so it's not so cluttered real quick.
To me this approach, as I read it, looks
easier to follow than the other approach,
but they're both really doing the same thing. It's just a matter of preference and documentation.
So that's it. I hope these two solutions were pretty close to yours.