学生 1 人が 1 つの構造体で、学籍番号 id と点数 score があります。バブルソートで sort_by_score(s, n) を完成させ、点数の高い順に並べましょう。点数が同じなら元の順番のままにします。show はポインタ p->id で学生 1 人を表示します。プログラムは 5 人を並べ替えて 1 行に 1 人表示します。
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++) {
// ここにコードを書く:前の点数が後ろより低ければ、2 人を丸ごと入れ替える
}
}
}
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;
}
実行すると例で試せます。よさそうなら提出しましょう。