用C语言编写:有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的平均成绩,以及平均成绩最高的学生的数据(包括学号、姓名、3门课成绩、平均分数)。
时间: 2023-12-15 08:23:03 浏览: 43
以下是用C语言编写的程序:
```
#include <stdio.h>
#include <string.h>
#define STUDENT_NUM 10
#define SUBJECT_NUM 3
struct Student {
int id;
char name[20];
int scores[SUBJECT_NUM];
float avg_score;
};
int main() {
struct Student students[STUDENT_NUM];
int i, j, highest_avg_index;
float subject_avgs[SUBJECT_NUM] = {0}, highest_avg = -1;
// 输入学生数据
for (i = 0; i < STUDENT_NUM; i++) {
printf("请输入第%d个学生的学号、姓名和3门课的成绩(以空格分隔):\n", i + 1);
scanf("%d %s", &students[i].id, students[i].name);
for (j = 0; j < SUBJECT_NUM; j++) {
scanf("%d", &students[i].scores[j]);
subject_avgs[j] += students[i].scores[j];
}
students[i].avg_score = (students[i].scores[0] + students[i].scores[1] + students[i].scores[2]) / 3.0;
}
// 计算科目平均分和最高平均分的学生
for (j = 0; j < SUBJECT_NUM; j++) {
subject_avgs[j] /= STUDENT_NUM;
}
for (i = 0; i < STUDENT_NUM; i++) {
if (students[i].avg_score > highest_avg) {
highest_avg = students[i].avg_score;
highest_avg_index = i;
}
}
// 打印结果
printf("3门课的平均成绩分别为:%.2f %.2f %.2f\n", subject_avgs[0], subject_avgs[1], subject_avgs[2]);
printf("平均成绩最高的学生是:\n");
printf("学号:%d\n姓名:%s\n成绩:%d %d %d\n平均分数:%.2f\n", students[highest_avg_index].id, students[highest_avg_index].name,
students[highest_avg_index].scores[0], students[highest_avg_index].scores[1], students[highest_avg_index].scores[2],
students[highest_avg_index].avg_score);
return 0;
}
```
运行程序后,依次输入每个学生的学号、姓名和3门课的成绩,程序会计算出3门课的平均成绩和平均成绩最高的学生的数据,并打印出来。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)