Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Conditions.

HOMEWORKTask 21 of 22 · step 9: Homework: exam resultsHomework: exam results
Homework: exam results in practice/02/

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.
Details & hint

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.

practice 02 main.c Code Diff
UNCHANGED This step does not change this file.
#include <limits.h>
#include <stdio.h>
#include "input.h"
int main(void) {
const int current_year = 2026;
int study_year = 1;
char group = 'A';
int birth_year = read_int("Year of birth: ");
int g1 = read_int("Grade 1: ");
int g2 = read_int("Grade 2: ");
int g3 = read_int("Grade 3: ");
double average = (g1 + g2 + g3) / 3.0;
int age = current_year - birth_year;
printf("Year of study: %d\n", study_year);
printf("Group: PI25%c\n", group);
printf("Age: %d years\n", age);
printf("Average: %.2f\n", average);
printf("In memory: int %zu B, double %zu B, char %zu B\n", sizeof study_year, sizeof average,
sizeof group);
printf("\n");
printf("Largest int: %d\n", INT_MAX);
long long century = 100LL * 365 * 24 * 60 * 60;
printf("100 years: %lld s\n", century);
printf("0.1 + 0.2 = %.17f\n", 0.1 + 0.2);
return 0;
}