C语言有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程成绩、平均分数)。
时间: 2023-12-15 08:45:04 浏览: 93
好的,这是一个关于C语言的问题。首先,我们可以定义一个结构体来存储学生的信息,包括学号、姓名和3门课程的成绩。然后,我们可以使用循环语句从键盘输入10个学生的数据,并计算出3门课程的总平均成绩。最后,我们可以遍历学生数据,找出最高分的学生并输出其信息。以下是示例代码:
```c
#include <stdio.h>
#include <string.h>
// 定义学生结构体
struct student {
int id;
char name[20];
float score[3];
float avg;
};
int main() {
struct student stu[10];
float total[3] = {0.0}; // 存储3门课程的总成绩
int max_index = 0; // 最高分学生的下标
// 输入10个学生的数据
for(int i = 0; i < 10; i++) {
printf("请输入第%d个学生的学号、姓名和3门课程的成绩:\n", i+1);
scanf("%d%s%f%f%f", &stu[i].id, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
// 计算3门课程的总成绩
total[0] += stu[i].score[0];
total[1] += stu[i].score[1];
total[2] += stu[i].score[2];
// 计算学生的平均分数
stu[i].avg = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
// 找出最高分学生的下标
if(stu[i].avg > stu[max_index].avg) {
max_index = i;
}
}
// 计算3门课程的平均分数
float avg[3];
avg[0] = total[0] / 10.0;
avg[1] = total[1] / 10.0;
avg[2] = total[2] / 10.0;
// 输出3门课程的平均分数
printf("3门课程的平均分数是:%.2f, %.2f, %.2f\n", avg[0], avg[1], avg[2]);
// 输出最高分学生的信息
printf("最高分学生的信息是:\n");
printf("学号:%d\n", stu[max_index].id);
printf("姓名:%s\n", stu[max_index].name);
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/20241231044833.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.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)