Skip to the task
Layout
Back to classic view

You are looking at lesson 3. Your place is still lesson 9, task 1: Strings.

HOMEWORKTask 21 of 22 · step 9: Homework: exam results

Homework: exam results

hw/03/main.c: read the students' points, turn them into grades with stars, and print how many passed and the best result.

Checklist

  • Every task is one commit with a clear message.

The folder hw/03 has a main.c stub and two tests.

Exam results: hw/03/main.c

Print the text exactly as below, because that is what the tests check.

  1. Ask How many students? . While the answer is less than 1: At least one student is needed. and ask again.
  2. For each student write the number (1. ) and ask Points: . While the points are not 0–100: Points must be between 0 and 100. and ask again (without the number now).
  3. Turn the points into a grade by the table and write Grade: , the grade, a space and as many stars * as the grade.
  4. After all the students, two lines: how many passed (a grade of 5 or more) and what share that is in percent, with one digit after the dot; then the best number of points.
Points 95–100 85–94 75–84 65–74 55–64 45–54 35–44 25–34 15–24 0–14
Grade 10 9 8 7 6 5 4 3 2 1

An example (hw/03/tests/1.in.txt):

How many students? 4
1. Points: 87
Grade: 9 *********
2. Points: 120
Points must be between 0 and 100.
Points: 97
Grade: 10 **********
3. Points: 44
Grade: 4 ****
4. Points: 63
Grade: 6 ******
Passed: 3 of 4 (75.0 %)
Best result: 97 points
  • The stars are a loop inside a loop: the inner for runs as many times as the grade.
  • You can write the table as an if / else if chain. There is a shorter way with / too: notice that the limits come every 10.
  • A % in a printf string is written %%.

Tests

The second test (2.in.txt) checks what the example doesn't: 0 students, negative points and the limits 0 and 100.

ctest --preset debug -L hw03

Handing in

Commit and push. Actions → Points: HW 3 (Exam results), 10 points when both tests pass.