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/03/

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 03 main.c Code Diff
UNCHANGED This step does not change this file.
#include <stdio.h>
#include "input.h"
int main(void) {
int count = read_int("How many grades? ");
while (count < 1) {
printf("At least one grade is needed.\n");
count = read_int("How many grades? ");
}
int sum = 0;
int lowest = 10;
int highest = 1;
int failed = 0;
for (int i = 1; i <= count; i++) {
printf("%d. ", i);
int grade = read_int("Grade: ");
while (grade < 1 || grade > 10) {
printf("Grade must be between 1 and 10.\n");
grade = read_int("Grade: ");
}
switch (grade) {
case 10:
printf("Excellent\n");
break;
case 9:
printf("Very good\n");
break;
case 8:
printf("Good\n");
break;
case 7:
printf("Fair\n");
break;
case 6:
printf("Satisfactory\n");
break;
case 5:
printf("Weak\n");
break;
default:
printf("Unsatisfactory\n");
break;
}
sum += grade;
if (grade < lowest) {
lowest = grade;
}
if (grade > highest) {
highest = grade;
}
if (grade < 5) {
failed++;
}
}
double average = (double)sum / count;
printf("Average: %.2f\n", average);
printf("Lowest: %d, highest: %d\n", lowest, highest);
printf("Failed: %d\n", failed);
return 0;
}