使用C语言编写的程序,实现输入10个学生的数据,计算3门课的总平均分,并找出最高分的学生数据。

输出入及输出结果图示
#include <stdio.h>

// 定义学生结构体
typedef struct Student {
int id;
char name[20];
float score1;
float score2;
float score3;
} Student;

// 输入学生数据
void input(Student *students, int n) {
for (int i = 0; i < n; i++) {
printf(“请输入第%d个学生的学号:”, i + 1);
scanf("%d", &(students[i].id));
printf(“请输入第%d个学生的姓名:”, i + 1);
scanf("%s", students[i].name);
printf(“请输入第%d个学生的3门课的成绩:”, i + 1);
scanf("%f%f%f", &(students[i].score1), &(students[i].score2), &(students[i].score3));
}
}

// 计算总平均分
float average(Student *students, int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += students[i].score1 + students[i].score2 + students[i].score3;
}
return sum / (n * 3);
}

// 找出最高分的学生数据
Student max(Student *students, int n) {
int maxIndex = 0;
float maxScore = students[0].score1 + students[0].score2 + students[0].score3;
for (int i = 1; i < n; i++) {
float totalScore = students[i].score1 + students[i].score2 + students[i].score3;
if (totalScore > maxScore) {
maxIndex = i;
maxScore = totalScore;
}
}
return students[maxIndex];
}

int main() {
Student students[10];
// 输入学生数据
input(students, 10);
// 计算总平均分
float avgScore = average(students, 10);
printf(“3门课的总平均成绩为:%.2f\n”, avgScore);
// 找出最高分的学生数据
Student maxStudent = max(students, 10);
printf(“最高分的学生信息:\n”);
printf(“学号:%d\n”, maxStudent.id);
printf(“姓名:%s\n”, maxStudent.name);
printf(“成绩:%.2f %.2f %.2f\n”, maxStudent.score1, maxStudent.score2, maxStudent.score3);
printf(“平均分数:%.2f\n”, (maxStudent.score1 + maxStudent.score2 + maxStudent.score3) / 3);
return 0;
}
输出结果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值