输入3个学生4门课的成绩,分别用函数实现以下功能:(1)计算每个学生的平均分。(2)计算每门课的平均分。(3)找出12个分数中最高分数所对应的学生和课程。用c语言表达,要简短
时间: 2024-02-17 20:00:46 浏览: 12
代码如下:
```c
#include <stdio.h>
float calcAverage(float scores[], int numCourses) {
float sum = 0;
for (int i = 0; i < numCourses; i++) {
sum += scores[i];
}
float average = sum / numCourses;
return average;
}
float calcCourseAverage(float scores[][4], int courseIndex, int numStudents) {
float sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += scores[i][courseIndex];
}
float average = sum / numStudents;
return average;
}
void findMax(float scores[][4], int numStudents, int numCourses, int* maxStudent, int* maxCourse) {
float maxScore = -1;
for (int i = 0; i < numStudents; i++) {
for (int j = 0; j < numCourses; j++) {
if (scores[i][j] > maxScore) {
maxScore = scores[i][j];
*maxStudent = i + 1;
*maxCourse = j + 1;
}
}
}
}
int main() {
float scores[3][4];
// 输入3个学生4门课的成绩
// ...
// 计算每个学生的平均分并输出
for (int i = 0; i < 3; i++) {
printf("Average score of student %d: %.2f\n", i + 1, calcAverage(scores[i], 4));
}
// 计算每门课的平均分并输出
for (int i = 0; i < 4; i++) {
printf("Average score of course %d: %.2f\n", i + 1, calcCourseAverage(scores, i, 3));
}
// 找出最高分数所对应的学生和课程并输出
int maxStudent, maxCourse;
findMax(scores, 3, 4, &maxStudent, &maxCourse);
printf("The highest score is %.2f, achieved by student %d in course %d.\n", scores[maxStudent - 1][maxCourse - 1], maxStudent, maxCourse);
return 0;
}
```