[MUSIC PLAYING] ROB BOWDEN: Hi. I'm Rob. And let's get Greedy. So the first thing we need to do is ask the user exactly how much change is owed. So here, we see we have a do/while loop. And we're setting dollars equal to GetFloat. What is GetFloat? It's one of the functions in the CS50 library that gets a float from the user. Remember, in order to use that function, we need to hash include CS50.h at the top. So once we have that value from the user, we also need to be sure that it's a valid value. We can't owe negative money. And so that's the purpose of this do/while loop. We continue looping while dollars is less than zero. And a do/while loop is the right thing to use here, since we need to ask the user at least once for how much money is owed. So once we have that number of dollars, we see here we have int cents equals round dollars times CENTS_PER_DOLLAR. At the top, we see that CENTS_PER_DOLLAR is sensibly defined as 100. So what is this line doing? Well, if you remember, floating point values aren't quite precise. Unlike integers, we can't represent floating point values exactly. There's always some sort of imprecision. So we prefer to work with just integers throughout this problem. And here, if the user entered $3.42, we're converting that to 342 cents and rounding, just get rid of any of that imprecision. So once we have the number of cents in an integer, we can continue with the rest of the program. We see here that we're declaring integer coins which we're only to use to keep track of the total number of coins. Here, we have our first while loop. We see while cents is greater than or equal to quarter, which above, is hash defined as 25, while that is true, we want to increment our number of coins and decrement cents by quarter. Remember that this syntax is equivalent to cents equals cents minus quarter. Those are the same. So what is this while loop doing? The idea here is that, if I know $3.42 is owed, I can continue giving quarters until I can't give quarters any more. I can't give quarters any more, once I've given $3.25. So then, once that's the case, we'll break out of this while loop. Cents will be left at 17 cents. And we'll continue down to the next while loop where we say, while cents is greater than or equal to dime. And now we're doing the same exact thing we did in the quarter case, except with dimes. So with $0.17, we'll loop until we can no longer give a dime, which is exactly once. And then we'll be left with 7 cents. Then we'll continue on to nickels, which will loop until we can't give any more nickels, which will leave us with two cents. And then, down at the bottom, we have pennies, which will loop and will finally leave us with zero cents. Then at the end, we just need to print out our number of coins. So this program is perfectly correct. But we can actually do a bit better. Now if I say that I owe you $10,000, you shouldn't need to go here's one quarter, two quarters, three quarters. You should know immediately that I owe you 40,000 quarters. Now let's look at a program that handles it a bit better. In this version of things, we still need to ask the user for the amount of change that they want in exactly the same way we did before. We need to round it exactly the way we did before. And we still have our coins integer declared exactly the same as before. So here's where things get a bit different. We're doing coins plus equals cents divided by quarter where quarter is 25. What this is saying is, take as many quarters as can go into cents and add that to coins. So if cents is 142, 142 divided by 25 gives us 5. Remember that integer division automatically truncates. So we're doing coins plus equals 5. Immediately after this, we're saying cents equal cents mod quarter. Remember that the mod operator gives us the remainder after division. So 142 mod quarter, that will give is 142 minus 125, which is 17. That's the remainder after doing 142 divided by 25. So now cents is equal to 17. And we do the same exact thing for dimes. 17 divided by 10 will give us 1. And we add that to coins. And then we update cents to be 17 mod 10, which is 7. And then the same for nickels. 7 divided by 5 is 1. Add that to coins. And then 7 mod 5 is 2. And that's our cents. And then, for pennies, there's no real point in dividing or modding, since, if we have $0.2 left over, we can just immediately add that to our number of coins. And finally, we need to print out our number of coins and, optionally, return 0 at the end of our program to signify everything worked. My name is Rob. And this was Greedy. [MUSIC PLAYING]