有n个结构体变量,内含学生学号,姓名和三门课程的成绩,要去输出平均成绩最高的学生的信息
时间: 2024-01-01 21:06:40 浏览: 34
可以通过遍历所有学生的成绩,计算每个学生的平均成绩并记录最高平均成绩的学生信息,最后输出该学生信息即可。示例代码如下:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 100
struct Student {
char id[20]; // 学号
char name[20]; // 姓名
double score1; // 第一门课程成绩
double score2; // 第二门课程成绩
double score3; // 第三门课程成绩
double avg; // 平均成绩
};
int main() {
struct Student students[MAX_STUDENT];
int n, i, max_index;
double max_avg = 0;
printf("请输入学生人数:");
scanf("%d", &n);
// 输入所有学生信息
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%s", students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("第一门课程成绩:");
scanf("%lf", &students[i].score1);
printf("第二门课程成绩:");
scanf("%lf", &students[i].score2);
printf("第三门课程成绩:");
scanf("%lf", &students[i].score3);
// 计算平均成绩
students[i].avg = (students[i].score1 + students[i].score2 + students[i].score3) / 3;
}
// 查找平均成绩最高的学生
for (i = 0; i < n; i++) {
if (students[i].avg > max_avg) {
max_avg = students[i].avg;
max_index = i;
}
}
// 输出平均成绩最高的学生信息
printf("平均成绩最高的学生信息如下:\n");
printf("学号:%s\n", students[max_index].id);
printf("姓名:%s\n", students[max_index].name);
printf("第一门课程成绩:%.2lf\n", students[max_index].score1);
printf("第二门课程成绩:%.2lf\n", students[max_index].score2);
printf("第三门课程成绩:%.2lf\n", students[max_index].score3);
printf("平均成绩:%.2lf\n", students[max_index].avg);
return 0;
}
```
该示例代码定义了一个 `Student` 结构体,包含学生的学号、姓名和三门课程的成绩,以及计算平均成绩的函数。在主函数中,先输入所有学生的信息,并计算每个学生的平均成绩。然后,遍历所有学生的平均成绩,找到最高平均成绩的学生信息,最后输出该学生信息即可。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)