用c语,实现功能:有10个学生变量,内含 学生学号、姓名和3门课程的成绩。要求输出 平均成绩最高的学生的信息(包括学号、姓 名、3门课程成绩和平均成绩)。
时间: 2023-05-11 16:04:25 浏览: 46
以下是用C语言实现的代码:
#include <stdio.h>
struct student {
int id;
char name[20];
float score1;
float score2;
float score3;
float avg_score;
};
int main() {
struct student stu[10];
int i, max_index;
float max_score = 0;
// 输入学生信息
for (i = 0; i < 10; i++) {
printf("请输入第%d个学生的学号:", i+1);
scanf("%d", &stu[i].id);
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", stu[i].name);
printf("请输入第%d个学生的三门课程成绩:", i+1);
scanf("%f%f%f", &stu[i].score1, &stu[i].score2, &stu[i].score3);
stu[i].avg_score = (stu[i].score1 + stu[i].score2 + stu[i].score3) / 3;
if (stu[i].avg_score > max_score) {
max_score = stu[i].avg_score;
max_index = i;
}
}
// 输出平均成绩最高的学生信息
printf("平均成绩最高的学生信息如下:\n");
printf("学号:%d\n", stu[max_index].id);
printf("姓名:%s\n", stu[max_index].name);
printf("三门课程成绩:%f %f %f\n", stu[max_index].score1, stu[max_index].score2, stu[max_index].score3);
printf("平均成绩:%f\n", stu[max_index].avg_score);
return 0;
}