Skip to content

Checkpoint: A Daily Summary

The Course
•
30 min study

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

Predict before running C
#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
Text
Total: 30

Now 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 int per 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. / 60 and % 60 split minutes into whole hours and leftover minutes.
  • Floating-point division. / 60.0 keeps the fraction for the hours line.
  • Matching placeholders. %d for integers, %02d for a padded clock field, %.2f for a double.

Plan before you type

  1. Write the expected output for the durations 25, 10 and 40 on paper: the total, the clock time and the hours.
  2. Declare the three durations, each with its value.
  3. Declare total as the sum of the three variables, not as the number 75.
  4. Print the total line and run. Compare it with your paper before adding more.
  5. Add the clock line and the hours line, running after each one.
  6. 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.00 instead of 1.25. The division was int / int, for example double hours = total / 60;: the fraction was dropped before the result was stored. Make one operand a double with 60.0.
  • Clock prints 1:5. The minutes field needs %02d so it always shows two digits.
  • A printf line prints nonsense, or a local build rejects it. A placeholder does not match its argument, for example %d given total / 60.0. Match %.2f with the double expression.
  • 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

Exercise

Your turn

Store the durations 25, 10 and 40 in three variables. Calculate the total once and print this report from it:

Text
Total: 75 minutes
Clock: 1:15
Hours: 1.25

Then set all three durations to 0 and check that the report prints Total: 0 minutes, Clock: 0:00 and Hours: 0.00.

Your workspace C
#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
C
#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;
}
Text
Total: 75 minutes
Clock: 1:15
Hours: 1.25

Every 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.

Was this article helpful?

Your response is saved on this device.