如何在 C 编程中使用二维数组找到最高和最低平均值?

How to find the highest and the lowest averages using 2D arrays in c programming?

我的代码中有一个错误,但我找不到位置。它必须在读取输入后计算平均值。 它读取所有代码并获取所有输入,但在计算平均值时它什么也不做。请帮忙

#include <stdlib.h>
#include <stdio.h>

int main() {
    int students, modules, m, n, first_student, last_student, l = 0;
    float student[100][20], high = 0, low = 20, average[students], average_mark[students];
    printf("Please enter the number of students:\n");
    scanf("%d", &students);
    printf("Please enter the number of modules:\n");
    scanf("%d", &modules);

    for (m = 0; m < students; ++m) {
        for (n = 0; n < modules; n++) {
            printf("Please enter the mark of module %d for student number %d,\n", n + 1, m + 1);
            scanf("%f", &student[m][n]);
        }
    }

    for (m = 0; m < students; ++m) {
        average[m] = 0;
    }

    for (n = 0; n < modules; n++) {
        average[m] += student[m][n];
        average_mark[m] = average[m] / modules;
    }

    printf("student     average\n");

    for (m = 0; m < students; ++m) {
        printf("%d     %f\n", m + 1, average_mark[m]);
    }

    for (m = 0; m < students; ++m) {
        if (average_mark[m] < low) {
            average_mark[m] = low;
        }
        else
            if (average_mark[m] == low) {
                last_student = m + 1;
            }

        if (average_mark[m] > high) {
            average_mark[m] = high;
        }
        else
            if (average_mark[m] == high) {
                first_student = m + 1;
            }
    }

    printf("The student who had the highest mark is %d : %f\n", first_student, high);
    printf("The student who had the lowest mark is %d : %f\n", last_student, low);

    for (m = 0; m < students; ++m) {
        if (average_mark[m] == 10 || average_mark[m] > 10) {
            l++;
        }
    }

    printf("the number of students having a mark that equals or exceeds the average is %d\n", l);

    return 0;
}

代码中还有一些其他问题,但作为开始,您可以先看看下面。

编辑:注意到并更正了下一个(第二个)问题。我要让你找到并纠正最后一个(第 3 个)问题。

// either these values need to be predefined
// or you'll need to dynamically allocate
// arrays using malloc

int MAX_STUDENTS = 100;
int MAX_MODULES = 20;

int main() {
    int students, modules, m, n, first_student, last_student, l = 0;
    float student[MAX_STUDENTS][MAX_MODULES], high = 0, low = 20, average[MAX_STUDENTS], average_mark[MAX_MODULES];
 
...
...
...

// the 2nd & 3rd for loops needs to be nested as below
    for (m = 0; m < students; ++m) { // this was 2nd
        average[m] = 0;

        for (n = 0; n < modules; n++) { // this was 3rd
            average[m] += student[m][n];
        }

        // you need to move this line out of 3rd loop
        average_mark[m] = average[m] / modules; 
    }

...
...
...
// this is how you find the lowest & highest ranking students
    for (m = 0; m < students; ++m) {
        if (average_mark[m] < low) {
            low = average_mark[m];
            last_student = m;
        }

        if (average_mark[m] > high) {
            high = average_mark[m];
            first_student = m;
        }
    }

    printf("The student who had the highest mark is %d : %f\n", first_student + 1, high);
    printf("The student who had the lowest mark is %d : %f\n", last_student + 1, low);

...
...
...
}