C语言有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩。从键盘输入10个学生的数据,要求输出3门课程的总平均成绩,以及最高分的学生的学号、姓名、3门课程成绩、平均分数。
时间: 2023-12-15 14:49:16 浏览: 122
好的,我明白了您的问题,以下是C语言的代码:
```c
#include<stdio.h>
#include<string.h>
struct Student{
int id; // 学号
char name[20]; // 姓名
float score[3]; // 3门成绩
float avg_score; // 平均分数
};
int main(){
struct Student s[10];
int i, max_index;
float total_score[3]={0}, max_score=-1;
for(i=0;i<10;i++){
printf("请输入学生%d的学号:\n", i+1);
scanf("%d",&s[i].id);
printf("请输入学生%d的姓名:\n", i+1);
scanf("%s",s[i].name);
printf("请输入学生%d的三门成绩:\n", i+1);
scanf("%f%f%f", &s[i].score[0], &s[i].score[1], &s[i].score[2]);
total_score[0] += s[i].score[0];
total_score[1] += s[i].score[1];
total_score[2] += s[i].score[2];
s[i].avg_score = (s[i].score[0] + s[i].score[1] + s[i].score[2])/3.0;
if(s[i].avg_score>max_score){
max_index = i;
max_score = s[i].avg_score;
}
}
printf("三门课程的总平均成绩为:%.1f %.1f %.1f\n", total_score[0]/10.0, total_score[1]/10.0, total_score[2]/10.0);
printf("最高分的学生是:\n");
printf("学号:%d 姓名:%s 三门成绩:%.1f %.1f %.1f 平均分数:%.1f\n", s[max_index].id, s[max_index].name, s[max_index].score[0], s[max_index].score[1], s[max_index].score[2], s[max_index].avg_score);
return 0;
}
```
这段代码首先定义了一个结构体类型 `Student`,里面包含学号、姓名、3门成绩以及平均分数。然后定义了一个长度为10的 `Student` 数组,并且使用循环从键盘输入每个学生的信息,同时累加每门课程的总分数以便最后求平均成绩。也在循环内部,计算当前学生的平均成绩,并且比较它与最高分并更新最高分。最后输出三门课程的总平均成绩,以及最高分学生的详细信息。
阅读全文
相关推荐

















