Skip to the task
Layout
Back to classic view

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

EDITTask 9 of 28 · step 5: SortingSort a copy
Sort a copy in practice/04/

The function sort_array (selection sort), a copy sorted, printing it and the median.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
Details & hint
  • The outer loop i from 0 to n - 2, the inner j looks for the smallest of the rest.
  • A swap through temp.
  • main: a copy with a loop, sort_array(sorted, DAYS), the median sorted[DAYS / 2].

Stuck? Sort the copy, not temperatures: the search has to return the day in the original order.

practice 04 main.c Code Diff
UNCHANGED This step does not change this file.
#include <stdio.h>
#include "input.h"
int main(void) {
int grade = read_int("Grade (0 - stop): ");
int sum = 0;
int count = 0;
while (grade != 0) {
sum += grade;
count++;
grade = read_int("Grade (0 - stop): ");
}
if (count > 0) {
printf("Average: %.2f\n", (double)sum / count);
} else {
printf("No grades.\n");
}
return 0;
}