0
Follow
0
View

The macro defines N and M to represent the number of students and courses, respectively.

donkey18 注册会员
2023-02-27 21:34

This answer quotes ChatGPT

Here is the C code for your requirements, including the macro definitions N and M, the global arrays score, a_stu, a_cour, and the definition and initialization of the global variables r and c:


#include 

#define N 5  // 学生数
#define M 3  // 课程数

float score[N][M] = {
    {80, 70, 90},
    {85, 76, 92},
    {78, 80, 85},
    {90, 92, 88},
    {82, 83, 86}
};  // 存放所有学生的课程分数

float a_stu[N];   // 存放每个学生的平均分
float a_cour[M];  // 存放每门课的平均分

int r = 0;  // 最高分所对应的学生
int c = 0;  // 最高分所对应的课程

int main() {
    // 计算每个学生的平均分
    for (int i = 0; i < N; i++) {
        float sum = 0;
        for (int j = 0; j < M; j++) {
            sum += score[i][j];
            if (score[i][j] > score[r][c]) {
                r = i;  // 更新最高分所对应的学生
                c = j;  // 更新最高分所对应的课程
            }
        }
        a_stu[i] = sum / M;
    }

    // 计算每门课的平均分
    for (int j = 0; j < M; j++) {
        float sum = 0;
        for (int i = 0; i < N; i++) {
            sum += score[i][j];
        }
        a_cour[j] = sum / N;
    }

    // 输出每个学生的平均分
    printf("每个学生的平均分为:\n");
    for (int i = 0; i < N; i++) {
        printf("学生%d的平均分为%.2f\n", i+1, a_stu[i]);
    }

    // 输出每门课的平均分
    printf("每门课的平均分为:\n");
    for (int j = 0; j < M; j++) {
        printf("第%d门课的平均分为%.2f\n", j+1, a_cour[j]);
    }

    // 输出最高分所对应的学生和课程
    printf("最高分为%.2f,是学生%d的第%d门课\n", score[r][c], r+1, c+1);

    return 0;
}

About the Author

Question Info

Publish Time
2023-02-27 21:34
Update Time
2023-02-27 21:34