Skip to the task
Layout
Back to classic view

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

EDITTask 10 of 22 · step 5: StatisticsStatistics
Statistics in practice/03/

At least one grade, then the lowest, the highest and how many failed.

Checklist

  • Every task is one commit with a clear message.

Numbered notes in the code explain the important lines.

Details & hint
  • Right after reading count: while (count < 1), as for a grade, so a division by 0 can no longer happen.
  • Three new variables before the loop and three ifs at the end of it.
  • lowest starts at 10 and highest at 1, so the first grade always replaces them.

Stuck? If lowest started at 0, no grade would be smaller, and the answer would always be 0.

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;
}