Skip to content

Extend It and Run Locally

The Course
•
30 min study

A finished program is one you can change safely. The first change most people want for the report is an average session length. It looks like one line, total divided by count, but it touches two of the week's hardest rules at once. With integers the fraction disappears, and with an empty report the divisor is zero. A small function with its own tests handles both, and it can be added to main.c without changing which sessions are accepted. The second half of the lesson shows how to build and run the project on your own computer.

Predict and run

Predict before running C
#include <stdio.h>

double average(int total, int count) {
    if (count == 0) {
        return 0.0;
    }
    return total / (double)count;
}

int main(void) {
    printf("%.1f %.1f\n", average(75, 3), average(0, 0));
    return 0;
}
Check your prediction
Text
25.0 0.0

75 divided by 3 is exactly 25. The empty report never reached the division, because the guard returned first. Try average(35, 2): it prints 17.5 because the division happens in floating point.

A cast changes one expression

(double)count is a cast. It converts the value of count to a double for this expression only; the variable itself is still an int. Since one operand is now a double, the division keeps its fraction. Where the cast goes matters:

Cast before dividing, not after C
#include <stdio.h>

int main(void) {
    int total = 35;
    int count = 2;
    printf("%.1f\n", (double)(total / count));
    printf("%.1f\n", (double)total / count);
    return 0;
}
Text
17.0
17.5

The first line casts the result of an integer division, after the half was already gone. The second casts total first, so the division itself is done in floating point. This is Day 1's rule again: the operands decide the arithmetic.

Why the empty case needs a decision

Without the guard, an empty report would compute 0 divided by 0.0. Integer division by zero is undefined behavior. Floating-point division by zero is defined on almost every system you will meet, but its result is "not a number", which printf shows as nan or -nan. Neither is a sensible line in a report. The brief does not say what the average of no sessions is, so the program must decide, and 0.0 matches the other zeros in an empty report. Writing that decision as an early return makes it visible and testable.

Add it to the report

This page has no multi-file editor. Use the "Study report" editor at the end of Assemble the Report, or the downloaded files below. Editors don't keep your changes when you leave a page, so copy your version out before you navigate. Define average in main.c above main, or declare it in report.h and define it in report.c if you want the test program to reach it too. Then add one line after the existing three: printf("Average: %.1f minutes\n", average(total_minutes(sessions, count), count));

Run the acceptance cases again. The first three lines must not change for any input: the extension adds information without changing which sessions are accepted. For the sample input the new line is Average: 71.7 minutes, and for empty input it is Average: 0.0 minutes.

Practice

Exercise

Your turn

Write the average helper and print the averages for (35, 2), (0, 0) and (215, 3), one per line with one decimal place.

Text
17.5
0.0
71.7
Your workspace C
#include <stdio.h>

int main(void) {
    // Write your solution here.
    return 0;
}
Hint 1: the idea

Handle the empty case before the division, and make sure the division is done in floating point.

Hint 2: a first step

Write average with no guard yet, returning total / count, and print average(35, 2) with %.1f. If it prints 17.0, the division happened between two ints before the result became a double: find which operand to convert. Add the guard when that line says 17.5.

Show one solution
C
#include <stdio.h>

double average(int total, int count) {
    if (count == 0) {
        return 0.0;
    }
    return total / (double)count;
}

int main(void) {
    printf("%.1f\n", average(35, 2));
    printf("%.1f\n", average(0, 0));
    printf("%.1f\n", average(215, 3));
    return 0;
}
Text
17.5
0.0
71.7

215 divided by 3 is 71.666..., and %.1f rounds it to 71.7 for display only; the returned double keeps the full value. The guard makes the empty report print a deliberate 0.0 instead of nan. Add the helper to your report editor and try empty input to see the same result there.

Optional: run on your computer

Download the C study report. The ZIP contains the same source files as the browser tabs, helper tests, sample input and a README. Your browser edits are not included in the download; copy your average extension into the downloaded files if you want to keep it.

Install a C17-capable Clang or GCC compiler: macOS Command Line Tools, your Linux distribution's compiler package, or a Windows toolchain such as MSYS2. Open a terminal in the extracted folder and check cc --version. In a POSIX shell, compile and run:

Shell
cc -std=c17 -Wall -Wextra -Wpedantic main.c report.c -o study-report
./study-report < sample-input.txt
cc -std=c17 -Wall -Wextra -Wpedantic test_report.c report.c -o test-report
./test-report

The first command compiles and links two source files into one program. -std=c17 selects the language version, and the three -W options turn on the warnings this course relies on; add -Werror to make every warning stop the build. The browser editor builds without these flags, so a local build is where the course's warnings actually appear. The < in the shell supplies the file's contents as standard input. Compile the test executable separately: it has its own main, so the two entry files must never be linked together. Do not define NDEBUG while testing, or every assertion disappears. After a run, echo $? shows the exit status: 0 for success, nonzero when an assertion failed or input could not be read.

On Windows, an executable may have an .exe suffix. The commands above work in a POSIX shell such as MSYS2; use its terminal for the input redirection. Local installation is optional, and the course's required exercises run in the browser.

Was this article helpful?

Your response is saved on this device.