输入十个学生的5门成绩,分别用函数实现下列功能
①计算每位学生的平均分 ②计算每门课的平均分 ③找出50个分数中最高的分数所对应的学生和课程
#include <stdio.h>
#define STUDENTS 10
#define SUBJECTS 5
typedef struct {
int scores[SUBJECTS];
float average;
} Student;
void calculateStudentAverage(Student students[], int numStudents) {
for (int i = 0; i < numStudents; i++) {
float sum = 0;
for (int j = 0; j < SUBJECTS; j++) {
sum += students[i].scores[j];
}
students[i].average = sum / SUBJECTS;
}
}
void calculateSubjectAverage(Student students[], int numStudents, float subjectAverages[]) {
for (int j = 0; j < SUBJECTS; j++) {
float sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += students[i].scores[j];
}
subjectAverages[j] = sum / numStudents;
}
}
void findHighestScore(Student students[], int numStudents, int* studentIndex, int* subjectIndex) {
int maxIndex = 0, maxSubjectIndex = 0;
int maxScore = students[0].scores[0];
for (int i = 0; i < numStudents; i++) {
for (int j = 0; j < SUBJECTS; j++) {
if (students[i].scores[j] > maxScore) {
maxScore = students[i].scores[j];
maxIndex = i;
maxSubjectIndex = j;
}
}
}
*studentIndex = maxIndex;
*subjectIndex = maxSubjectIndex;
}
int main() {
Student students[STUDENTS];
float subjectAverages[SUBJECTS];
int highestStudentIndex, highestSubjectIndex;
printf("请输入%d个学生的%d门成绩:\n", STUDENTS, SUBJECTS);
for (int i = 0; i < STUDENTS; i++) {
printf("学生%d的成绩:", i + 1);
for (int j = 0; j < SUBJECTS; j++) {
scanf("%d", &students[i].scores[j]);
}
}
calculateStudentAverage(students, STUDENTS);
calculateSubjectAverage(students, STUDENTS, subjectAverages);
findHighestScore(students, STUDENTS, &highestStudentIndex, &highestSubjectIndex);
printf("学生平均分:\n");
for (int i = 0; i < STUDENTS; i++) {
printf("学生%d: %.2f\n", i + 1, students[i].average);
}
printf("科目平均分:\n");
for (int j = 0; j < SUBJECTS; j++) {
printf("科目%d: %.2f\n", j + 1, subjectAverages[j]);
}
printf("最高分:%d(学生%d,科目%d)\n", students[highestStudentIndex].scores[highestSubjectIndex], highestStudentIndex + 1, highestSubjectIndex + 1);
return 0;
}