输入10名学生5门课的成绩,分别用函数求: 每门课的平均分。找出所有课程中最高的分数所对应的学生和课程。
#include <stdio.h>
// 计算每门课程的平均分
void calculateAvg(float score[][5], int numStudents) {
float sum;
for (int j = 0; j < 5; j++) {
sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += score[i][j];
}
printf("第%d门课的平均分为: %.2f\n", j + 1, sum / numStudents);
}
}
// 找到最高分数对应的学生和课程
void findMaxScore(float score[][5], int numStudents) {
float maxScore = 0;
int maxStuIndex, maxCourseIndex;
for (int i = 0; i < numStudents; i++) {
for (int j = 0; j < 5; j++) {
if (score[i][j] > maxScore) {
maxScore = score[i][j];
maxStuIndex = i;
maxCourseIndex = j;
}
}
}
printf("最高分数为:%.2f, 对应的学生为第%d位,课程为第%d门课\n", maxScore, maxStuIndex + 1, maxCourseIndex + 1);
}
int main() {
float score[10][5];
int numStudents = 10;
// 输入成绩
for (int i = 0; i < numStudents; i++) {
printf("请输入第%d位学生的5门课成绩:\n", i + 1);
for (int j = 0; j < 5; j++) {
scanf("%f", &score[i][j]);
}
}
// 计算每门课程的平均分
calculateAvg(score, numStudents);
// 找到最高分数对应的学生和课程
findMaxScore(score, numStudents);
return 0;
}