有5个学生,每个学生的数据包括学号、姓名、3门课的成绩。从键盘输入5个学生数据,要求输出3门课总平均成绩,以及平均成绩最高分的学生的数据(包括学号、姓名、3门课的成绩、平均分)用c语言
时间: 2023-12-16 21:28:31 浏览: 667
以下是C语言的代码实现,可以满足您的需求:
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int id;
float score[3];
float avg;
};
int main() {
struct Student stu[5];
float sum[3] = {0};
float max_avg = 0;
int max_index = 0;
// 输入学生信息
for (int i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息(姓名 学号 三门课成绩): ", i+1);
scanf("%s %d %f %f %f", stu[i].name, &stu[i].id, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
// 计算平均分
stu[i].avg = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3;
// 计算总分
sum[0] += stu[i].score[0];
sum[1] += stu[i].score[1];
sum[2] += stu[i].score[2];
// 找到平均分最高的学生
if (stu[i].avg > max_avg) {
max_avg = stu[i].avg;
max_index = i;
}
}
// 输出3门课的平均成绩
printf("三门课的平均成绩为: %.2f %.2f %.2f\n", sum[0]/5, sum[1]/5, sum[2]/5);
// 输出平均成绩最高的学生信息
printf("平均成绩最高的学生信息为:\n");
printf("姓名: %s\n", stu[max_index].name);
printf("学号: %d\n", stu[max_index].id);
printf("三门课成绩: %.2f %.2f %.2f\n", stu[max_index].score[0], stu[max_index].score[1], stu[max_index].score[2]);
printf("平均分数: %.2f\n", stu[max_index].avg);
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)