A checkpoint combines the day's ideas without adding new syntax. Your task is a small daily report for three study sessions: the total in minutes, the same total as a clock time, and the total in fractional hours. The important design rule is that the durations are stored once and everything else is calculated from them. If tomorrow's second session is 15 minutes instead of 10, you change one number and every line of the report updates itself.
There is a second lesson hidden in this checkpoint. The compiler accepts any program that follows the rules of C, including one whose arithmetic is wrong. Successful compilation tells you the text is valid C. Only a known expected answer tells you the program is correct.
Predict and run
#include <stdio.h>
int main(void) {
int first = 20;
int second = 10;
int total = first + second;
printf("Total: %d\n", total);
return 0;
}Check your prediction
Total: 30Now change + to - and run again. The program still compiles and prints Total: 10 without complaint. You only notice the bug because you already knew the answer should be 30. Restore the + before you continue. Every checkpoint in this course starts the same way: decide the expected output before you write the code.
What you will use
- Initialized variables. One
intper session, each given a value in its declaration. - A calculated total. One variable computed once from the sessions and reused by every line of the report.
- Integer division and remainder.
/ 60and% 60split minutes into whole hours and leftover minutes. - Floating-point division.
/ 60.0keeps the fraction for the hours line. - Matching placeholders.
%dfor integers,%02dfor a padded clock field,%.2ffor adouble.
Plan before you type
- Write the expected output for the durations 25, 10 and 40 on paper: the total, the clock time and the hours.
- Declare the three durations, each with its value.
- Declare
totalas the sum of the three variables, not as the number 75. - Print the total line and run. Compare it with your paper before adding more.
- Add the clock line and the hours line, running after each one.
- Change one duration and predict the new report before you run it.
Prove it can fail
Once your report matches, break it on purpose. Replace 60.0 with 60: now total / 60 is int / int, so the fraction is gone (it is 1), and the result is an int handed to %.2f. That mismatch is undefined behavior: a local build with warnings refuses it, and the browser prints nonsense such as 0.00. Either way the hours are wrong, and the fix is the same: keep 60.0. Then write int total = 75; instead of the sum and change the first duration to 30. The report still says 75, which proves a hard-coded total cannot follow the data. Restore both before moving on.
Mistakes worth checking for
- Hours prints
1.00instead of1.25. The division wasint / int, for exampledouble hours = total / 60;: the fraction was dropped before the result was stored. Make one operand adoublewith60.0. - Clock prints
1:5. The minutes field needs%02dso it always shows two digits. - A
printfline prints nonsense, or a local build rejects it. A placeholder does not match its argument, for example%dgiventotal / 60.0. Match%.2fwith thedoubleexpression. - Changing a duration does not change the report. The total, or one of the derived numbers, was typed in as a constant instead of calculated.
- A random-looking number appears. A variable was declared without a value and read before anything was stored in it. Initialize every variable where you declare it.
Practice
Your turn
Store the durations 25, 10 and 40 in three variables. Calculate the total once and print this report from it:
Total: 75 minutes
Clock: 1:15
Hours: 1.25Then set all three durations to 0 and check that the report prints Total: 0 minutes, Clock: 0:00 and Hours: 0.00.
#include <stdio.h>
int main(void) {
// Write your solution here.
return 0;
}Hint 1: the idea
Calculate the total once and reuse it. Each line of the report is a different view of the same number.
Hint 2: a first step
Write int total = first + second + third; and print only the first line. When it says 75, add the clock line using total / 60 and total % 60.
Show one solution
#include <stdio.h>
int main(void) {
int first = 25;
int second = 10;
int third = 40;
int total = first + second + third;
printf("Total: %d minutes\n", total);
printf("Clock: %d:%02d\n", total / 60, total % 60);
printf("Hours: %.2f\n", total / 60.0);
return 0;
}Total: 75 minutes
Clock: 1:15
Hours: 1.25Every number in the report is derived from total, and total is derived from the three sessions, so the report cannot disagree with its own data. With all zeros, integer division gives 0 hours and 0 minutes, and 0 / 60.0 is 0.0, so there is no special case to write. Tomorrow you will make the program take decisions, such as whether a goal was reached.
Completion is saved in this browser only. It does not measure mastery.