用c语言生产一串代码 输入5个学生4门课程的成绩,分别用函数实现下列功能: (1)计算每个学生的平均分;(2)计算每门课的平均分;(3)找出所有20个分数中最高的分数所对应的学生和课程;
时间: 2024-01-01 13:05:47 浏览: 60
```c
#include <stdio.h>
#define N 5 // 学生数量
#define M 4 // 课程数量
void calc_stu_avg(float scores[N][M], float avg[N]) {
for (int i = 0; i < N; i++) {
float sum = 0;
for (int j = 0; j < M; j++) {
sum += scores[i][j];
}
avg[i] = sum / M;
}
}
void calc_course_avg(float scores[N][M], float avg[M]) {
for (int j = 0; j < M; j++) {
float sum = 0;
for (int i = 0; i < N; i++) {
sum += scores[i][j];
}
avg[j] = sum / N;
}
}
void find_max_score(float scores[N][M], float* max_score, int* max_stu, int* max_course) {
*max_score = scores[0][0];
*max_stu = 0;
*max_course = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (scores[i][j] > *max_score) {
*max_score = scores[i][j];
*max_stu = i;
*max_course = j;
}
}
}
}
int main() {
float scores[N][M];
for (int i = 0; i < N; i++) {
printf("请输入第%d个学生的成绩:\n", i+1);
for (int j = 0; j < M; j++) {
scanf("%f", &scores[i][j]);
}
}
float avg_stu[N];
calc_stu_avg(scores, avg_stu);
printf("每个学生的平均分:\n");
for (int i = 0; i < N; i++) {
printf("第%d个学生的平均分:%.2f\n", i+1, avg_stu[i]);
}
float avg_course[M];
calc_course_avg(scores, avg_course);
printf("每门课的平均分:\n");
for (int j = 0; j < M; j++) {
printf("第%d门课的平均分:%.2f\n", j+1, avg_course[j]);
}
float max_score;
int max_stu, max_course;
find_max_score(scores, &max_score, &max_stu, &max_course);
printf("最高分数:%.2f,对应学生:第%d个,对应课程:第%d门\n", max_score, max_stu+1, max_course+1);
return 0;
}
```
阅读全文