Skip to the task
Layout
Back to classic view

You are looking at lesson 3. Your place is still lesson 4, task 1: Style.

RUNTask 11 of 22 · step 5: StatisticsRun it with the same input
Run it with the same input in practice/03/

Again 3, 9, 11, 4, 10. Then try 0 for "How many grades?".

Checklist

  • Every task is one commit with a clear message.

Numbered notes in the code explain the important lines.

Details & hint
$ .\build\debug\practice\03\p03_grades.exe
How many grades? 3
1. Grade: 9
Very good
2. Grade: 11
Grade must be between 1 and 10.
Grade: 4
Unsatisfactory
3. Grade: 10
Excellent
Average: 7.67
Lowest: 4, highest: 10
Failed: 1

You should see: Two new lines at the end. With 0 the program asks again: At least one grade is needed.

practice 03 main.c Code Diff +18
M · MODIFIED Green = add, blue = change, red mark = removed line.
#include <stdio.h>
#include "input.h"
int main(void) {
int count = read_int("How many grades? ");
while (count < 1) {
Note 1: At least one grade: no more division by 0.
printf("At least one grade is needed.\n");
count = read_int("How many grades? ");
}
int sum = 0;
int lowest = 10;
Note 2: Starts at the largest possible: the first grade replaces it.
int highest = 1;
Note 3: Starts at the smallest possible.
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++;
Note 4: ++ adds 1.
}
}
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;
}
opened for this task
$ .\build\debug\practice\03\p03_grades.exe
How many grades? 3
1. Grade: 9
Very good
2. Grade: 11
Grade must be between 1 and 10.
Grade: 4
Unsatisfactory
3. Grade: 10
Excellent
Average: 7.67
Lowest: 4, highest: 10
Failed: 1