计算每个学生的平均分: 定义一个函数,输入参数为一个学生的4门课成绩,返回值为该学生的平均分。在主程序中,分别输入3个学生的成绩,调用该函数计算每个学生的平均分并输出。 计算每门课的平均分: 定义一个函数,输入参数为所有学生在某门课的成绩,返回值为该门课的平均分。在主程序中,分别输入4门课的所有学生成绩,调用该函数计算每门课的平均分并输出。 找出12个分数中最高分数所对应的学生和课程: 定义一个函数,输入参数为3个学生4门课的成绩,返回值为最高分数所对应的学生和课程。在主程序中,输入3个学生4门课的成绩,调用该函数找出最高分数所对应的学生和课程并输出,用c语言,要简短
时间: 2024-02-17 16:00:45 浏览: 84
对于第一个问题,代码如下:
```c
float calcAverage(float score1, float score2, float score3, float score4) {
float average = (score1 + score2 + score3 + score4) / 4;
return average;
}
int main() {
float s1_score1, s1_score2, s1_score3, s1_score4;
float s2_score1, s2_score2, s2_score3, s2_score4;
float s3_score1, s3_score2, s3_score3, s3_score4;
// 输入每个学生的成绩
// ...
// 计算每个学生的平均分并输出
printf("Student 1's average score: %.2f\n", calcAverage(s1_score1, s1_score2, s1_score3, s1_score4));
printf("Student 2's average score: %.2f\n", calcAverage(s2_score1, s2_score2, s2_score3, s2_score4));
printf("Student 3's average score: %.2f\n", calcAverage(s3_score1, s3_score2, s3_score3, s3_score4));
return 0;
}
```
对于第二个问题,代码如下:
```c
float calcCourseAverage(float scores[], int numStudents) {
float sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += scores[i];
}
float average = sum / numStudents;
return average;
}
int main() {
float course1_scores[3], course2_scores[3], course3_scores[3], course4_scores[3];
// 输入每门课的所有学生成绩
// ...
// 计算每门课的平均分并输出
printf("Course 1's average score: %.2f\n", calcCourseAverage(course1_scores, 3));
printf("Course 2's average score: %.2f\n", calcCourseAverage(course2_scores, 3));
printf("Course 3's average score: %.2f\n", calcCourseAverage(course3_scores, 3));
printf("Course 4's average score: %.2f\n", calcCourseAverage(course4_scores, 3));
return 0;
}
```
对于第三个问题,代码如下:
```c
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门课的成绩
// ...
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;
}
```
阅读全文