BETA Try the new lesson view
Code

A colleague's program

A colleague sent you a program: it reads grades until 0 is typed and prints their average. "It works, it just shows odd things sometimes." This time you can copy the file: it isn't your code, and your job is to tidy it up.

Create practice/04:

add_executable(p04_average main.c)
target_link_libraries(p04_average PRIVATE course_input)

and main.c with your colleague's code (on the right).

Run it

The program compiles without a single warning. Type 8, 9, 10 and 0:

Grade (0 - stop): 8
Grade (0 - stop): 9
Grade (0 - stop): 10
Grade (0 - stop): 0
Average: 6.75

(8 + 9 + 10) / 3 = 9, not 6.75. And if you type 0 straight away, the program writes Average: 0.00, though there were no grades.

Where is the bug? Code like this is hard even to read. First you tidy it, then you look for the bug. Commit: Add classmate's program.