有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出31 ]课的平均成绩,以及平均成绩最高的学生的数据(包括学号、姓名、3门课成绩、平均分数)。用c语言写
时间: 2023-12-15 13:10:47 浏览: 27
好的,以下是用 C 语言实现的代码:
```c
#include <stdio.h>
#include <string.h>
#define NUM_STUDENTS 10
#define NUM_COURSES 3
struct student {
char id[20];
char name[20];
float grades[NUM_COURSES];
float average;
};
int main() {
struct student students[NUM_STUDENTS];
int i, j;
float sum = 0.0, max_avg = 0.0;
int max_idx = 0;
// 输入学生信息
for (i = 0; i < NUM_STUDENTS; i++) {
printf("请输入第 %d 个学生的信息:\n", i + 1);
printf("学号:");
scanf("%s", students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("成绩:");
for (j = 0; j < NUM_COURSES; j++) {
scanf("%f", &students[i].grades[j]);
sum += students[i].grades[j];
}
students[i].average = sum / NUM_COURSES;
sum = 0.0;
}
// 计算平均成绩和平均分数最高的学生
for (i = 0; i < NUM_STUDENTS; i++) {
sum += students[i].grades[2];
if (students[i].average > max_avg) {
max_avg = students[i].average;
max_idx = i;
}
}
float avg = sum / NUM_STUDENTS;
// 打印结果
printf("\n31课的平均成绩为:%.2f\n", avg);
printf("平均成绩最高的学生信息如下:\n");
printf("学号:%s\n", students[max_idx].id);
printf("姓名:%s\n", students[max_idx].name);
printf("成绩:%.2f %.2f %.2f\n", students[max_idx].grades[0], students[max_idx].grades[1], students[max_idx].grades[2]);
printf("平均分数:%.2f\n", students[max_idx].average);
return 0;
}
```
运行程序后,依次输入每个学生的信息,即可得到31课的平均成绩和平均成绩最高的学生的信息。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)