名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把分数最高的学生数据放在b所指的数组中,注意:分数最高的学生可能不止一个,函数返回分数最高的学生的人数。
时间: 2024-06-12 20:07:22 浏览: 8
```C
#include <stdio.h>
#include <string.h>
#define MAX_NUM 100
typedef struct {
char name[20];
int id;
float score;
} Student;
int fun(Student s[], int n, Student b[]) {
int max_num = 0;
float max_score = s[0].score;
for (int i = 0; i < n; i++) {
if (s[i].score > max_score) {
max_score = s[i].score;
max_num = 0;
}
if (s[i].score == max_score) {
b[max_num] = s[i];
max_num++;
}
}
return max_num;
}
int main() {
Student s[MAX_NUM] = {{"Tom", 1, 90}, {"Jack", 2, 80}, {"Mary", 3, 95}, {"John", 4, 95}};
Student b[MAX_NUM];
int max_num = fun(s, 4, b);
for (int i = 0; i < max_num; i++) {
printf("%s %d %.2f\n", b[i].name, b[i].id, b[i].score);
}
return 0;
}
```