1 00:00:05,600 --> 00:00:08,900 Okay so welcome to the section 8 challenge solution. 2 00:00:09,200 --> 00:00:11,290 I hope you were able to solve this problem, 3 00:00:11,290 --> 00:00:14,790 and I hope it was fun to do. And I hope you tested it and 4 00:00:14,790 --> 00:00:18,090 tweaked it, made it your own,added some features to it and so forth. 5 00:00:18,090 --> 00:00:21,790 Let me just go through a couple of solutions. First, we'll do the solution 6 00:00:21,790 --> 00:00:23,990 without using the modulo operator. 7 00:00:23,990 --> 00:00:27,990 So I'm going to scroll up here, and let me just explain what I've got going on. Here's 8 00:00:27,990 --> 00:00:29,490 my main, right at the beginning. 9 00:00:30,490 --> 00:00:35,090 Okay. So I've defined conversion constants. These are just simple integers, 10 00:00:35,390 --> 00:00:39,190 and it's the value in cents of a dollar, a quarter, a dime and a nickel. 11 00:00:39,190 --> 00:00:42,850 Okay. So a dollar is 100 cents, a quarter is 25 cents and so forth. 12 00:00:43,650 --> 00:00:46,250 Then I've got another integer called change amount. 13 00:00:46,250 --> 00:00:48,850 This is what I'm asking the user to enter. 14 00:00:49,450 --> 00:00:52,750 All right. So let's suppose that we're entering 92 here, 15 00:00:52,750 --> 00:00:54,450 and we'll walk through a simple example. 16 00:00:56,450 --> 00:00:59,250 Here, I'm declaring all the variables that I'm going to need: 17 00:00:59,250 --> 00:01:03,150 dollars, quarters, dimes, nickels and pennies are going to contain 18 00:01:03,150 --> 00:01:07,510 the number of dollars, quarters, dimes, nickels and pennies that I need to provide the change. 19 00:01:07,510 --> 00:01:10,110 And then I'm going to have another variable called balance, 20 00:01:10,110 --> 00:01:13,910 and that's going to be a balance, a running balance. So every time that I figure out 21 00:01:13,910 --> 00:01:17,910 I need two dollars and I'm going to subtract two dollars from the original amount 22 00:01:17,910 --> 00:01:19,270 until I get down to zero. 23 00:01:19,770 --> 00:01:22,870 Okay. So that's my algorithm or my solution 24 00:01:22,870 --> 00:01:27,170 is basically to determine how much of the highest currency I need 25 00:01:27,170 --> 00:01:30,970 and then subtract that and have a running balance until it becomes zero. 26 00:01:31,410 --> 00:01:33,290 All right. So let's do this example here. 27 00:01:33,290 --> 00:01:35,890 And I'm just going to write some letters over here. I'll write dollars, 28 00:01:36,290 --> 00:01:39,490 quarters, dimes, nickels and pennies. 29 00:01:39,490 --> 00:01:40,690 So we can keep it running. 30 00:01:41,190 --> 00:01:43,890 And I'll have a balance variable here as well. 31 00:01:45,890 --> 00:01:48,990 All right. So let's start. We're right over here on line 57, 32 00:01:49,490 --> 00:01:51,390 and we need to figure out how many dollars 33 00:01:51,390 --> 00:01:54,690 are in 92 cents. Well, there are none, right. 34 00:01:54,690 --> 00:01:59,190 So what we want to do is we want to divide the change amount which in this case is 92 35 00:01:59,850 --> 00:02:03,650 divide that by the dollar value which is a 100 cents per dollar. 36 00:02:04,650 --> 00:02:06,850 In this case, that's going to give me a 0. 37 00:02:06,850 --> 00:02:10,210 Remember, we're using integer division here. So I'm going to get to 0, 38 00:02:10,210 --> 00:02:11,710 which says no dollars. 39 00:02:12,210 --> 00:02:14,710 Makes sense, right because I've got less than 100 cents. 40 00:02:15,510 --> 00:02:18,110 Now I need to update my balance. 41 00:02:18,610 --> 00:02:23,410 Well, how do I do that? I decrement however many dollars times a 100. 42 00:02:23,410 --> 00:02:27,610 Well, I had 0 here, right. So in this case, the change amount is going to be 92 43 00:02:28,210 --> 00:02:30,210 and I'm going to subtract from that 44 00:02:30,910 --> 00:02:32,910 0 times 100. 45 00:02:35,110 --> 00:02:39,410 Okay. So in this case, I'm not subtracting anything. My balance becomes 92. 46 00:02:41,410 --> 00:02:42,970 Okay. So that's the first step. 47 00:02:42,970 --> 00:02:45,570 Now we need to determine how many quarters we need. 48 00:02:45,570 --> 00:02:48,170 So I'm going to take that balance, which is 92. 49 00:02:48,770 --> 00:02:52,220 And I'm going to divide it by the quarter's value which is 25 cents. 50 00:02:52,720 --> 00:02:54,320 And that will give me a 3, 51 00:02:55,020 --> 00:02:58,020 right. 75 cents so I need 3 quarters. 52 00:02:58,380 --> 00:03:02,740 Now what I want to do is I want to subtract that 75 cents from the 92. 53 00:03:02,940 --> 00:03:06,740 And in this case, I'm using a compound operator right here, the minus equals. 54 00:03:06,740 --> 00:03:10,840 So all I'm saying is balance equals balance minus the right-hand side. 55 00:03:10,840 --> 00:03:13,240 Well, the right-hand side is 3 quarters 56 00:03:13,240 --> 00:03:17,040 times 25 cents each, which is the 75. 57 00:03:17,540 --> 00:03:20,440 Okay. So I'm going to subtract 75 from the balance, 58 00:03:21,040 --> 00:03:24,040 which is going to give me 17. So that's my current balance. 59 00:03:24,540 --> 00:03:27,440 Now I can work on the dimes. How many dimes? 60 00:03:27,440 --> 00:03:31,640 Well, it's the balance which is 17 divided by 10 cents per dime. 61 00:03:32,140 --> 00:03:36,240 So this is going to give me 1. So that's 1 dime. 62 00:03:36,900 --> 00:03:39,900 And I'm going to do the same thing here. Hopefully, you can see what's going on. 63 00:03:39,900 --> 00:03:43,800 I'm going to subtract 1 dime which is 10 cents each from the balance. 64 00:03:43,800 --> 00:03:47,400 So I'm going to subtract 10 from the balance, gives me 7. 65 00:03:48,400 --> 00:03:50,390 Now I'm going to figure out how many nickels I need, 66 00:03:50,390 --> 00:03:54,790 7 divided by 5 cents per nickel. So I need 1 nickel. 67 00:03:56,490 --> 00:04:00,090 I subtract 5 from the 7, and I end up with 2. 68 00:04:00,490 --> 00:04:02,790 My balance is the number of pennies I need. 69 00:04:02,790 --> 00:04:06,090 I don't need to do any more calculations, and that gives me the 2 pennies, 70 00:04:06,450 --> 00:04:10,850 and we're basically done. So if we add up all of this, let's test this out, 71 00:04:10,850 --> 00:04:14,250 this is going to give me 0, this is going to give me 25 72 00:04:14,250 --> 00:04:16,750 times 3 which is 75 cents, 73 00:04:19,750 --> 00:04:22,750 this will give me the 75, this will give me 10, 74 00:04:22,750 --> 00:04:26,410 1 times 10, 1 times 5, and 2 times 1. 75 00:04:26,910 --> 00:04:30,570 If I add all those guys up, I should have 75, 85, 92, 76 00:04:31,170 --> 00:04:32,530 exactly what I expected. 77 00:04:34,030 --> 00:04:37,910 Okay. So that gives you an example of one way to approach this. 78 00:04:37,910 --> 00:04:40,680 We'll do it really quickly with another example here. 79 00:04:40,680 --> 00:04:43,480 Let's say we wanted to use something like 267. 80 00:04:44,280 --> 00:04:48,720 So let me clear this, and I'll go through this one really quickly this time. 81 00:04:48,720 --> 00:04:52,320 So let's say it's 267 is the amount that the user entered. 82 00:04:53,820 --> 00:04:58,120 Okay. So here, how many dollars? Well, 267 83 00:04:58,670 --> 00:05:02,920 divided by 100, right. 100 cents 84 00:05:02,920 --> 00:05:05,920 dollar that's going to give me 2 dollars. 85 00:05:06,420 --> 00:05:10,020 Then what I want to do is I want to subtract, I'll run my balance up here 86 00:05:11,020 --> 00:05:12,220 those 200 87 00:05:12,820 --> 00:05:15,620 which gives me 67 cents. That's my balance. 88 00:05:15,620 --> 00:05:17,920 How many quarters in 67 cents, 89 00:05:19,620 --> 00:05:23,820 divide that by 25, that gives me 2 quarters, 90 00:05:24,620 --> 00:05:27,620 which is 50 cents. I'm subtracting 50 cents, 91 00:05:27,620 --> 00:05:30,920 and we're right back to where we were with the other problem where we've got 17. 92 00:05:31,320 --> 00:05:35,420 How many dimes divide that by 10, it gives me 1 dime, 93 00:05:35,920 --> 00:05:39,820 that's 10 cents. I subtract 10 from that. I end up with 7. 94 00:05:40,220 --> 00:05:43,920 How many nickels, divide that by five. It gives me 1 nickel. 95 00:05:44,280 --> 00:05:47,880 And I've got 2 left, which is my pennies, 96 00:05:49,380 --> 00:05:50,980 right. So this gives us 97 00:05:50,980 --> 00:05:54,280 right here 100 times 2, it gives us the 200. 98 00:05:55,380 --> 00:05:59,180 This gives me 50. This gives me 10, 5 and 2. 99 00:05:59,780 --> 00:06:03,540 Add all that up, I get 250, 260, 270. 100 00:06:04,340 --> 00:06:07,340 I'm sorry 200, 250, 267, 101 00:06:08,330 --> 00:06:10,630 which is exactly what we started out with. 102 00:06:11,630 --> 00:06:14,330 Okay now we can take advantage of something here. 103 00:06:14,330 --> 00:06:16,930 If you notice if I have 267 104 00:06:18,230 --> 00:06:22,590 and I divide that by 100, let's say, that gives me 2 dollars integer division. 105 00:06:23,190 --> 00:06:26,290 But notice that I have a remainder. 106 00:06:26,290 --> 00:06:28,490 My remainder is 67, right. 107 00:06:28,490 --> 00:06:32,370 That's where we can use the modulo operator to rework this a little bit. 108 00:06:32,370 --> 00:06:33,470 So we'll do that next. 109 00:06:34,270 --> 00:06:38,970 Okay. So let's look at another solution. This time we'll be using the modulo operator. 110 00:06:39,630 --> 00:06:43,400 So suppose the user enters 267 111 00:06:43,400 --> 00:06:44,900 for the change amount. 112 00:06:46,900 --> 00:06:50,400 And I've put all these on the same file. That way you can 113 00:06:50,400 --> 00:06:52,400 play around with both of them a lot easier. 114 00:06:52,400 --> 00:06:56,060 So in this case, I'm zeroing everything out just like when we started. 115 00:06:56,060 --> 00:06:59,660 So I've got 267 cents, that's what the user has entered, 116 00:07:00,160 --> 00:07:03,460 and we'll just keep a running total over here for the dollars, 117 00:07:03,860 --> 00:07:06,460 the quarters, the dimes, the nickels and the pennies. 118 00:07:06,460 --> 00:07:11,060 Okay. So how many dollars? Well, we divide 267 by 119 00:07:11,060 --> 00:07:14,260 100 cents per dollar, and that gives us the 2 dollars. 120 00:07:14,860 --> 00:07:17,560 So we definitely need that. Now rather than subtract 121 00:07:17,560 --> 00:07:21,260 the 2 times 100 from the 267 as we did before, 122 00:07:21,260 --> 00:07:23,760 we can take advantage of the modulo operator. 123 00:07:23,760 --> 00:07:27,060 So in this case, the change amount is 267 124 00:07:27,760 --> 00:07:29,760 mod 100, right. 125 00:07:30,420 --> 00:07:34,620 This gives us 2 with a remainder of 67. 126 00:07:35,020 --> 00:07:37,120 So the effect is exactly like 127 00:07:37,120 --> 00:07:39,820 subtracting 200 from 267. 128 00:07:40,020 --> 00:07:44,520 Okay. So what we can do is we'll keep our balance down here this time just to have a little bit more room. 129 00:07:45,120 --> 00:07:48,220 In this case, our balance is going to be 67. 130 00:07:48,220 --> 00:07:49,920 That's what we just assigned right here. 131 00:07:51,220 --> 00:07:55,620 Now let's figure out how many quarters we have. Well, our balance is 67, 132 00:07:56,120 --> 00:08:00,320 divide that by 25 cents per quarter and that gives us 2 quarters. 133 00:08:02,120 --> 00:08:05,420 So now again instead of subtracting 50 from 67, 134 00:08:05,420 --> 00:08:08,820 we could just use the modular operator. Here, I'm using the compound operator. 135 00:08:08,820 --> 00:08:12,120 So it's balance equals balance mod quarter value. 136 00:08:12,420 --> 00:08:15,420 Same idea. So balance is 67 137 00:08:16,020 --> 00:08:18,320 mod 25, 138 00:08:19,820 --> 00:08:22,370 gives us 2 quarters right. That's 50 139 00:08:22,570 --> 00:08:26,970 with a remainder of 17. So our balance becomes 17. 140 00:08:27,670 --> 00:08:30,270 And then from this point forward, I think you could see what's happening. 141 00:08:30,270 --> 00:08:32,870 Our balance is now 17 142 00:08:33,470 --> 00:08:37,070 divided by 10 cents per dime, gives us 1 dime. 143 00:08:38,870 --> 00:08:42,870 And then we do the same modular operator. So we're going to have 17 144 00:08:42,870 --> 00:08:44,670 mod 10, 145 00:08:44,670 --> 00:08:46,930 gives us 1 with a remainder of 7. 146 00:08:46,930 --> 00:08:49,530 That's really what we care about as a remainder in this case. 147 00:08:50,190 --> 00:08:51,290 How many nickels, 148 00:08:51,790 --> 00:08:54,790 7 divided by 5 cents per nickel, 149 00:08:54,790 --> 00:08:56,390 it gives us 1 nickel. 150 00:08:57,690 --> 00:09:01,390 And then we do the mod operator one last time, we're going to say 151 00:09:01,390 --> 00:09:04,390 the balance would be 7, 152 00:09:04,390 --> 00:09:08,890 which is my balance, mod 5 which gives me a 1 with a remainder of 2. 153 00:09:09,690 --> 00:09:12,990 At that point, our balance is 2 which is the number of pennies that are left. 154 00:09:13,590 --> 00:09:17,990 And again if we add this up, we're going to have 2 dollars here, which is 200. 155 00:09:19,290 --> 00:09:23,090 We've got 50 cents here, 10 cents here, 156 00:09:23,340 --> 00:09:26,030 5 cents here and 2 cents here, 157 00:09:26,030 --> 00:09:29,020 which gives us 200, 250, 267. 158 00:09:31,120 --> 00:09:35,890 okay. Another approach to me this approach -- I'll clear this so it's not so cluttered real quick. 159 00:09:35,890 --> 00:09:39,190 To me this approach, as I read it, looks 160 00:09:39,190 --> 00:09:41,590 easier to follow than the other approach, 161 00:09:41,590 --> 00:09:45,690 but they're both really doing the same thing. It's just a matter of preference and documentation. 162 00:09:46,190 --> 00:09:50,490 So that's it. I hope these two solutions were pretty close to yours.