Each student is a struct: an ID id and a score score. Use bubble sort to finish sort_by_score(s, n), sorting from highest score to lowest; students with the same score keep their order. show uses the pointer p->id to show one student. The program sorts 5 students and shows one per line.
A02 95 A05 95 A04 88 A01 78 A03 62
#include <stdio.h>
struct Student {
char id[8];
int score;
};
void sort_by_score(struct Student s[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
// your code here: if the first has a lower score than the next, swap the whole students
}
}
}
void show(const struct Student *p) {
printf("%s %d\n", p->id, p->score);
}
int main(void) {
struct Student s[] = {{"A01", 78}, {"A02", 95}, {"A03", 62}, {"A04", 88}, {"A05", 95}};
sort_by_score(s, 5);
for (int i = 0; i < 5; i++) {
show(&s[i]);
}
return 0;
}
Run tries the examples. When they look right, Submit.