1 00:00:00,000 --> 00:00:00,510 2 00:00:00,510 --> 00:00:02,040 [? AMILAH: ?] Ready to cash out? 3 00:00:02,040 --> 00:00:04,620 Let's say you're owed back $0.32. 4 00:00:04,620 --> 00:00:08,670 The cashier hands you 32 pennies worth $0.01 each. 5 00:00:08,670 --> 00:00:14,340 Well, perhaps you might rather they hand you three dimes and two pennies-- 6 00:00:14,340 --> 00:00:16,410 also $0.32. 7 00:00:16,410 --> 00:00:21,630 Can you think of a way where the cashier could hand you back even fewer coins? 8 00:00:21,630 --> 00:00:24,180 We're going to implement that. 9 00:00:24,180 --> 00:00:29,090 Central to this algorithm is the concept of using the largest coin possible. 10 00:00:29,090 --> 00:00:34,770 The four available coins to us are quarters, worth $0.25, dimes, 11 00:00:34,770 --> 00:00:41,070 worth $0.10, nickels, worth $0.05, and pennies, worth $0.01 each. 12 00:00:41,070 --> 00:00:41,970 OK. 13 00:00:41,970 --> 00:00:46,920 So let's go back to our example of being owed $0.32. 14 00:00:46,920 --> 00:00:50,130 So let's ask ourselves first, can a quarter be used? 15 00:00:50,130 --> 00:00:53,820 Yes, because $0.32 is larger than $0.25. 16 00:00:53,820 --> 00:00:58,650 And so we're now owed $0.07, and we've used one coin. 17 00:00:58,650 --> 00:01:00,420 Can we use another quarter? 18 00:01:00,420 --> 00:01:04,709 The answer is no because $0.07 is less than $0.25, 19 00:01:04,709 --> 00:01:08,130 and so we proceed to the next largest coin-- 20 00:01:08,130 --> 00:01:09,300 a dime. 21 00:01:09,300 --> 00:01:14,730 Well, $0.07 is less than $0.10, and so we can't use a dime. 22 00:01:14,730 --> 00:01:19,110 We go on, then, to a nickel, and a nickel worth $0.05 23 00:01:19,110 --> 00:01:22,110 is less than the $0.07 that we're owed back. 24 00:01:22,110 --> 00:01:29,220 And so we can indeed use one more coin, and now we're owed back $0.02. 25 00:01:29,220 --> 00:01:33,300 If we ask ourselves whether we can use that same coin, the answer is no. 26 00:01:33,300 --> 00:01:37,210 The value that we're owed back is less than a nickel. 27 00:01:37,210 --> 00:01:40,350 So then we go to the last coin available. 28 00:01:40,350 --> 00:01:41,790 Can we use a penny? 29 00:01:41,790 --> 00:01:44,700 And the answer is that yes, indeed, we'll 30 00:01:44,700 --> 00:01:50,070 be using two more pennies for a total of four coins used. 31 00:01:50,070 --> 00:01:53,880 And so you'll see, if you were to run this exact same problem with the staff 32 00:01:53,880 --> 00:01:59,640 solution and eventually yours, then running the cash problem with $0.32 33 00:01:59,640 --> 00:02:02,970 would get you an output of four. 34 00:02:02,970 --> 00:02:03,660 All right. 35 00:02:03,660 --> 00:02:06,730 So what are our tasks for this problem? 36 00:02:06,730 --> 00:02:09,240 First, we want to prompt the user for an amount of change 37 00:02:09,240 --> 00:02:10,650 that they're owed back. 38 00:02:10,650 --> 00:02:12,900 Then, we want to implement an algorithm that 39 00:02:12,900 --> 00:02:15,750 always uses the largest coin possible. 40 00:02:15,750 --> 00:02:21,840 We want to keep track of the coins used, and print that final number of coins. 41 00:02:21,840 --> 00:02:25,320 So let's talk about prompting the user first. 42 00:02:25,320 --> 00:02:27,990 Whenever we prompt the user, we have to make sure 43 00:02:27,990 --> 00:02:33,000 that we only accept values that will make sense for our problem. 44 00:02:33,000 --> 00:02:37,470 In this case, you'll also find the get float function in the CS50 library 45 00:02:37,470 --> 00:02:39,270 quite useful. 46 00:02:39,270 --> 00:02:43,290 Here's a snippet of code that will ensure that the user provides you 47 00:02:43,290 --> 00:02:44,910 with a valid float. 48 00:02:44,910 --> 00:02:49,020 Here, I have a do-while loop that will execute the body of my loop at least 49 00:02:49,020 --> 00:02:53,160 once, prompting the user using the get float function found in the CS50 50 00:02:53,160 --> 00:02:54,540 library. 51 00:02:54,540 --> 00:02:58,450 After that, as long as that float is invalid, 52 00:02:58,450 --> 00:03:01,050 then I'll continue to re-prompt the user. 53 00:03:01,050 --> 00:03:03,870 If, however, that float is valid, then I'll 54 00:03:03,870 --> 00:03:06,970 accept it and move on to the rest of my code. 55 00:03:06,970 --> 00:03:10,630 So now we have a dollar value from the user. 56 00:03:10,630 --> 00:03:12,840 However, that's stored as a float, and we 57 00:03:12,840 --> 00:03:16,210 know that floats have floating point in precision. 58 00:03:16,210 --> 00:03:20,280 So in order to avoid that, I might convert those floats-- 59 00:03:20,280 --> 00:03:22,960 the dollar values-- into integers-- 60 00:03:22,960 --> 00:03:24,180 cents. 61 00:03:24,180 --> 00:03:26,010 So look up the round function. 62 00:03:26,010 --> 00:03:28,000 This might come in handy. 63 00:03:28,000 --> 00:03:32,070 Now, we can advance to some pseudocode for the cash problem. 64 00:03:32,070 --> 00:03:35,070 After getting the amount in dollars that the user is owed 65 00:03:35,070 --> 00:03:37,890 and converting that to cents, then we can proceed 66 00:03:37,890 --> 00:03:41,400 with using the largest coin available. 67 00:03:41,400 --> 00:03:45,630 While quarters can be used, we can increase the count of coins used 68 00:03:45,630 --> 00:03:50,040 and decrease the amount owed by a quarter every time. 69 00:03:50,040 --> 00:03:54,060 Once quarters can no longer be used, we'll move on to dimes, 70 00:03:54,060 --> 00:03:56,730 and then nickels, and then pennies, finally 71 00:03:56,730 --> 00:03:58,950 printing the number of coins used. 72 00:03:58,950 --> 00:04:02,820 One way to implement this pseudocode might be to use greater than and less 73 00:04:02,820 --> 00:04:06,510 than operators, as well as addition and subtraction, 74 00:04:06,510 --> 00:04:09,750 but I also want to propose another way of solving 75 00:04:09,750 --> 00:04:12,690 this problem using modulo math. 76 00:04:12,690 --> 00:04:16,500 The modulo operator returns the remainder of two integers 77 00:04:16,500 --> 00:04:18,510 after division. 78 00:04:18,510 --> 00:04:26,600 Some examples-- 50 modulo 5 gives you 0 because 5 is a factor of 50. 79 00:04:26,600 --> 00:04:29,250 50 modulo 10 also gives you 0. 80 00:04:29,250 --> 00:04:32,610 There's no remainder after dividing those two numbers. 81 00:04:32,610 --> 00:04:36,990 Any number modded with itself will also give you 0. 82 00:04:36,990 --> 00:04:39,570 Now, let's talk about a non-zero case. 83 00:04:39,570 --> 00:04:46,830 50 modulo 49 gives you one because 49 goes into 50 once, 84 00:04:46,830 --> 00:04:50,610 and then the remainder of that is just 1. 85 00:04:50,610 --> 00:04:56,370 53 modulo 50, now, gives you a remainder of 3. 86 00:04:56,370 --> 00:05:00,770 Let's go back to our example where the user is owed back $0.32. 87 00:05:00,770 --> 00:05:04,400 Our first question is to ask whether or not we can use quarters. 88 00:05:04,400 --> 00:05:08,240 What I want you to do now is go through the same example, filling 89 00:05:08,240 --> 00:05:13,580 in the change owed box and the coins used box using the modulo and division 90 00:05:13,580 --> 00:05:15,680 operators. 91 00:05:15,680 --> 00:05:16,580 All right. 92 00:05:16,580 --> 00:05:20,960 Now, you have two proposed ways of implementing this cash problem. 93 00:05:20,960 --> 00:05:23,870 We've prompted the user for some amount of change. 94 00:05:23,870 --> 00:05:28,340 We have used the largest coin possible, keeping track of those coins, 95 00:05:28,340 --> 00:05:32,570 so the last step is to print that final number. 96 00:05:32,570 --> 00:05:35,030 In order to print the value of a variable, 97 00:05:35,030 --> 00:05:39,140 I'll need to use a placeholder for whatever variable type that is. 98 00:05:39,140 --> 00:05:43,910 In this case, to print an integer, I use the placeholder for an integer-- %i-- 99 00:05:43,910 --> 00:05:47,330 and then pass in that variable. 100 00:05:47,330 --> 00:05:48,170 All right. 101 00:05:48,170 --> 00:05:50,330 And now you can print the final number of coins 102 00:05:50,330 --> 00:05:53,600 used to give the user their change. 103 00:05:53,600 --> 00:05:57,190 My name is [? Amilah, ?] and this was cash. 104 00:05:57,190 --> 00:05:58,468