Skip to the task
Layout
Back to classic view

You are looking at lesson 4. Your place is still lesson 7, task 1: Modules.

HOMEWORKTask 22 of 23 · step 10: Homework: the scholarship

Homework: the scholarship

hw/04/main.c: a colleague's program with three bugs. Find them with the debugger, fix them, tidy the code and write up DEBUGGING.md.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).

This time you don't write from scratch. The folder hw/04 holds a colleague's program that compiles but calculates wrongly.

The scholarship: hw/04/main.c

The program asks how many grades there will be, reads them and prints the average, the lowest grade and whether the student gets a scholarship: an average of 8.5 or more and no grade below 6.

How many grades? 4
Grade: 9
Grade: 8
Grade: 10
Grade: 8
Average: 8.75
Lowest: 8
Scholarship: yes

There are three bugs in the code. Your job:

  1. Find the bugs with the debugger. Not by guessing: a breakpoint, F8, and in the Variables window watch where a value first differs from the one you expect.
  2. Fix them. Don't rewrite everything: each bug is fixed in one line.
  3. Tidy the code: Ctrl+Alt+L, clear names (Shift+F6), { }, the #include order as in the other topics.
  4. Write up hw/04/DEBUGGING.md: for each bug, the line, what the debugger showed and how you fixed it.

Each of these in its own commit. The lecturer will see in the history how you searched.

Tests

Two output tests and one file test: DEBUGGING.md has all three sections and no TODO left.

ctest --preset debug -L hw04

With the program unfixed, the first test ends like this:

=== hw04_scholarship_1: FAILED ===
The program ended with code 1 (must be 0).
The output differs on line 6.
  expected: |Average: 8.75|
  got:      |Grade:~|
  (\t = tab, ~ = a space at the end of the line)

--- error output (stderr) ---

The input ended.

That is already a first hint: after four grades the program asks for a fifth, and the input has already ended.

The tests don't check style: the lecturer grades the code, the names and what DEBUGGING.md says.

Handing in

Commit and push. Actions → Points: HW 4 (Scholarship), 10 points when all three tests pass.