[? AMILAH: ?] Ready to cash out?
Let's say you're owed back $0.32.
The cashier hands you 32 pennies worth $0.01 each.
Well, perhaps you might rather they hand you three dimes and two pennies--
also $0.32.
Can you think of a way where the cashier could hand you back even fewer coins?
We're going to implement that.
Central to this algorithm is the concept of using the largest coin possible.
The four available coins to us are quarters, worth $0.25, dimes,
worth $0.10, nickels, worth $0.05, and pennies, worth $0.01 each.
OK.
So let's go back to our example of being owed $0.32.
So let's ask ourselves first, can a quarter be used?
Yes, because $0.32 is larger than $0.25.
And so we're now owed $0.07, and we've used one coin.
Can we use another quarter?
The answer is no because $0.07 is less than $0.25,
and so we proceed to the next largest coin--
a dime.
Well, $0.07 is less than $0.10, and so we can't use a dime.
We go on, then, to a nickel, and a nickel worth $0.05
is less than the $0.07 that we're owed back.
And so we can indeed use one more coin, and now we're owed back $0.02.
If we ask ourselves whether we can use that same coin, the answer is no.
The value that we're owed back is less than a nickel.
So then we go to the last coin available.
Can we use a penny?
And the answer is that yes, indeed, we'll
be using two more pennies for a total of four coins used.
And so you'll see, if you were to run this exact same problem with the staff
solution and eventually yours, then running the cash problem with $0.32
would get you an output of four.
All right.
So what are our tasks for this problem?
First, we want to prompt the user for an amount of change
that they're owed back.
Then, we want to implement an algorithm that
always uses the largest coin possible.
We want to keep track of the coins used, and print that final number of coins.
So let's talk about prompting the user first.
Whenever we prompt the user, we have to make sure
that we only accept values that will make sense for our problem.
In this case, you'll also find the get float function in the CS50 library
quite useful.
Here's a snippet of code that will ensure that the user provides you
with a valid float.
Here, I have a do-while loop that will execute the body of my loop at least
once, prompting the user using the get float function found in the CS50
library.
After that, as long as that float is invalid,
then I'll continue to re-prompt the user.
If, however, that float is valid, then I'll
accept it and move on to the rest of my code.
So now we have a dollar value from the user.
However, that's stored as a float, and we
know that floats have floating point in precision.
So in order to avoid that, I might convert those floats--
the dollar values-- into integers--
cents.
So look up the round function.
This might come in handy.
Now, we can advance to some pseudocode for the cash problem.
After getting the amount in dollars that the user is owed
and converting that to cents, then we can proceed
with using the largest coin available.
While quarters can be used, we can increase the count of coins used
and decrease the amount owed by a quarter every time.
Once quarters can no longer be used, we'll move on to dimes,
and then nickels, and then pennies, finally
printing the number of coins used.
One way to implement this pseudocode might be to use greater than and less
than operators, as well as addition and subtraction,
but I also want to propose another way of solving
this problem using modulo math.
The modulo operator returns the remainder of two integers
after division.
Some examples-- 50 modulo 5 gives you 0 because 5 is a factor of 50.
50 modulo 10 also gives you 0.
There's no remainder after dividing those two numbers.
Any number modded with itself will also give you 0.
Now, let's talk about a non-zero case.
50 modulo 49 gives you one because 49 goes into 50 once,
and then the remainder of that is just 1.
53 modulo 50, now, gives you a remainder of 3.
Let's go back to our example where the user is owed back $0.32.
Our first question is to ask whether or not we can use quarters.
What I want you to do now is go through the same example, filling
in the change owed box and the coins used box using the modulo and division
operators.
All right.
Now, you have two proposed ways of implementing this cash problem.
We've prompted the user for some amount of change.
We have used the largest coin possible, keeping track of those coins,
so the last step is to print that final number.
In order to print the value of a variable,
I'll need to use a placeholder for whatever variable type that is.
In this case, to print an integer, I use the placeholder for an integer-- %i--
and then pass in that variable.
All right.
And now you can print the final number of coins
used to give the user their change.
My name is [? Amilah, ?] and this was cash.