Course 1.3Lesson 5 of 5
In this project we write two small programs that use the whole course. A change maker works out how many notes of 100, 50, 20, 10 and 5 and how many coins of 1 rupee are needed for an amount, by using // for how many and % for what is left, from the biggest note to the smallest. A clock program turns a number of seconds into hours, minutes and seconds. Both programs are checked by hand, by testing with other numbers, and by looking at the edge cases.
Try the code and the simulations beside the lesson.
After this lesson you can
// and % as a pair to split an amount into groups,A shop must pay back 387 rupees in change. It has notes of 100, 50, 20, 10 and 5 rupees and coins of 1 rupee. We want the fewest pieces. How many of each?
Start with the biggest note and take as many as you can.
The answer is: 3 of 100, 1 of 50, 1 of 20, 1 of 10, 1 of 5 and 2 of 1.
Each step has the same two actions. How many of this note fit? That is //. What is left? That is %. So every step is a pair: // and %.
A program that works for one number may fail for another. Test with these.
amount = 100. The answer must be one note of 100, and nothing else.amount = 0. Everything must be 0.amount = 4. Only coins: four of 1 rupee.amount = 995. Nine hundreds, one fifty, two twenties, zero tens, one five. Check it by hand.A video lasts 10000 seconds. How long is it in hours, minutes and seconds?
The idea is the same as for the change: the biggest unit first. An hour has 3600 seconds, and a minute has 60 seconds.
Check by hand: 2 * 3600 + 46 * 60 + 40 = 7200 + 2760 + 40 = 10000. It works. This kind of check, putting the parts back together, is a good test for any program that splits a number.
The slider stops just before 12 hours, so the hour hand goes round once. A whole day would need a 24 hour clock.
total_seconds = 10000 to 90, to 3600 and to 0. Is each answer right?days = hours // 24 and hours = hours % 24. Try 100000 seconds.amount. Then every note works on the whole amount./ instead of //. You get 3.87 hundreds, and not 3.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
// tells how many, % tells what is left. Use them as a pair, from the biggest unit to the smallest.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
/ wrong in a change maker?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.