有一个班4个学生,5门课程。一:求第1门课程的平均分;二:找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;三:找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。
#include <stdio.h>
#define NUM_STUDENTS 4
#define NUM_COURSES 5
void averageFirstCourse(int scores[NUM_STUDENTS][NUM_COURSES]) {
double total = 0;
for (int i = 0; i < NUM_STUDENTS; i++) {
total += scores[i][0];
}
printf("Average score of the first course: %f\n", total / NUM_STUDENTS);
}
void findFailingStudents(int scores[NUM_STUDENTS][NUM_COURSES]) {
for (int i = 0; i < NUM_STUDENTS; i++) {
int failCount = 0;
for (int j = 0; j < NUM_COURSES; j++) {
if (scores[i][j] < 60) {
failCount++;
}
}
if (failCount >= 2) {
printf("Student %d failed in %d courses. Scores: ", i + 1, failCount);
double average = 0;
for (int j = 0; j < NUM_COURSES; j++) {
printf("%d ", scores[i][j]);
average += scores[i][j];
}
printf("Average: %f\n", average / NUM_COURSES);
}
}
}
void findExcellentStudents(int scores[NUM_STUDENTS][NUM_COURSES]) {
for (int i = 0; i < NUM_STUDENTS; i++) {
double average = 0;
int allAbove85 = 1;
for (int j = 0; j < NUM_COURSES; j++) {
average += scores[i][j];
if (scores[i][j] < 85) {
allAbove85 = 0;
}
}
average /= NUM_COURSES;
if (average > 90 || allAbove85) {
printf("Student %d is excellent. Scores: ", i + 1);
for (int j = 0; j < NUM_COURSES; j++) {
printf("%d ", scores[i][j]);
}
printf("Average: %f\n", average);
}
}
}
int main() {
int scores[NUM_STUDENTS][NUM_COURSES] = {
{95, 85, 76, 67, 88},
{55, 78, 88, 93, 91},
{45, 56, 73, 66, 80},
{89, 92, 97, 81, 86}
};
averageFirstCourse(scores);
findFailingStudents(scores);
findExcellentStudents(scores);
return 0;
}
代码解释:
averageFirstCourse
函数计算第1门课程的平均分。findFailingStudents
函数找出有两门以上课程不及格的学生并输出详细信息。findExcellentStudents
函数找出平均成绩在90分以上或全部课程成绩在85分以上的学生。main
函数中初始化一个班4个学生,5门课程的成绩矩阵,并调用这三个函数进行处理。