Skip to the task
Layout
Back to classic view

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

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 git diff: red = old code, green = new.
--- a/practice/03/main.c
+++ b/practice/03/main.c
@@ -4,7 +4,14 @@
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);
@@ -39,9 +46,20 @@
}
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;
}
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