Time Parked
Time for a small project! In this checkpoint, you'll write a whole program on your own, using what you've learned in the first three lessons: printf, variables, calculations with whole numbers and conditions.
The exercises here are a bit different from the ones so far. The instructions say what the program should print, not how to write it. There are many correct ways, so pick the one that makes sense to you. If you get stuck, look back at the lesson where you learned what you need.
The project. A parking garage needs a program for its pay machine. Over three exercises, your program will work out how long a car was parked, what the parking costs and how much change the machine gives back. Each exercise continues where the previous one ended: the editor starts with a finished program from the previous step, which may look different from yours. That's fine.
The data. The variables at the top of main say when the car drove in and when it drove out, in hours and minutes: it came at 7:50 and left at 10:15. In a real pay machine, these times would come from the parking ticket. Here, the program gets them from the variables, and your code should work with the variables, not with their numbers copied into it: when you change a variable, the program should still print the right result. To check that, each exercise also runs your program with other values, and it has to get those right too.
The task. From 7:50 to 10:15, the car was parked for 145 minutes, which is 2 hours and 25 minutes. The garage charges for every hour that has started, even if the car left a few minutes into it, so this car pays for 3 hours. The program prints:
Parked: 2 h 25 min
Started hours: 3A tip: it's easier to count everything in minutes since midnight: 7:50 is 7 × 60 + 50 = 470 minutes. Then remember / and % with whole numbers from the lesson on variables.
Watch out for a car that leaves after whole hours. From 7:50 to 10:50, it's 3 hours and 0 minutes, and that's 3 started hours, not 4.
Instructions
Print how long the car was parked, in hours and minutes, as in the example.
Below that, print how many hours have started.
Start programming for free
1/3