输入3个学生4门课的成绩,分别用函数实现以下功能:(1)计算每个学生的平均分。(2)计算每门课的平均分。(3)找出12个分数中最高分数所对应的学生和课程。,c语言代码
时间: 2024-02-17 19:00:41 浏览: 13
以下是基于数组的 C 语言代码实现:
```c
#include <stdio.h>
// 计算每个学生的平均分
void avg_score_per_student(float scores[][4], int num_students) {
for(int i = 0; i < num_students; i++) {
float sum = 0;
for(int j = 0; j < 4; j++) {
sum += scores[i][j];
}
float avg = sum / 4;
printf("The average score of student %d is %.2f\n", i+1, avg);
}
}
// 计算每门课的平均分
void avg_score_per_course(float scores[][4], int num_courses) {
for(int j = 0; j < num_courses; j++) {
float sum = 0;
for(int i = 0; i < 3; i++) {
sum += scores[i][j];
}
float avg = sum / 3;
printf("The average score of course %d is %.2f\n", j+1, avg);
}
}
// 找出12个分数中最高分数所对应的学生和课程
void max_score(float scores[][4], int num_students, int num_courses) {
float max_score = 0;
int max_student = 0, max_course = 0;
for(int i = 0; i < num_students; i++) {
for(int j = 0; j < num_courses; j++) {
if(scores[i][j] > max_score) {
max_score = scores[i][j];
max_student = i+1;
max_course = j+1;
}
}
}
printf("The highest score is %.2f, obtained by student %d in course %d\n", max_score, max_student, max_course);
}
int main() {
// 输入学生的成绩
float scores[3][4];
for(int i = 0; i < 3; i++) {
printf("Enter scores for student %d: ", i+1);
for(int j = 0; j < 4; j++) {
scanf("%f", &scores[i][j]);
}
}
// 计算每个学生的平均分
avg_score_per_student(scores, 3);
// 计算每门课的平均分
avg_score_per_course(scores, 4);
// 找出12个分数中最高分数所对应的学生和课程
max_score(scores, 3, 4);
return 0;
}
```