有N个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入N个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)用C语言
时间: 2023-12-15 19:47:28 浏览: 76
以下是一个示例代码,其中使用了结构体来存储每个学生的数据:
```c
#include <stdio.h>
#include <string.h>
#define MAX_N 100
struct Student {
char id[20];
char name[20];
int scores[3];
};
int main() {
int n;
printf("请输入学生数:");
scanf("%d", &n);
struct Student students[MAX_N];
memset(students, 0, sizeof(students));
double sum[3] = {0}; // 用于存储每门课的总成绩
int max_score = -1; // 用于存储最高分
int max_index = -1; // 用于存储最高分所在的学生在数组中的下标
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的数据(学号 姓名 成绩1 成绩2 成绩3):", i + 1);
scanf("%s %s %d %d %d", students[i].id, students[i].name, &students[i].scores[0], &students[i].scores[1], &students[i].scores[2]);
for (int j = 0; j < 3; j++) {
sum[j] += students[i].scores[j]; // 更新每门课的总成绩
}
int total_score = students[i].scores[0] + students[i].scores[1] + students[i].scores[2];
if (total_score > max_score) {
max_score = total_score;
max_index = i;
}
}
printf("三门课的平均成绩为:%.2f %.2f %.2f\n", sum[0] / n, sum[1] / n, sum[2] / n);
printf("最高分学生的数据为:%s %s %d %d %d\n", students[max_index].id, students[max_index].name, students[max_index].scores[0], students[max_index].scores[1], students[max_index].scores[2]);
return 0;
}
```
执行程序后,可以按照提示输入每个学生的数据,最终输出三门课的平均成绩以及最高分学生的数据。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![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)