Files
workspace/c/2.c
e2hang ebcee63b7c New
2026-01-09 00:05:37 +08:00

36 lines
699 B
C

#include <stdio.h>
#include <stdlib.h>
typedef struct{
int id;
char name[100];
int a, b, c;
double average;
} Student;
void calc(Student* st){
double sum = st->a + st->b + st->c;
sum /= 3;
st->average = sum;
}
int cmp(const void* a, const void* b){
return ((Student*)a)->average - ((Student*)b)->average;
}
int main(){
Student st[] = {
{1, "Chang", 10, 9, 8},
{2, "Wan", 1, 2, 4},
{3, "Liu", 2, 3, 5}
};
for(int i = 0; i < 3; i++){
calc(&st[i]);
}
qsort(st, 3, sizeof(Student), cmp);
for(int i = 0; i < 3; ++i){
printf("Name: %s, Average: %.6lf\n", st[i].name, st[i].average);
}
return 0;
}