简体   繁体   中英

columns average C

I have the next function in which i want to find the average of the N columns, but after finishing the M lines i take wrong inputs in the averages_days array, any idea?

int day_max_average(int a[M][N]) {
    int max = 0, day, i, j, averages_days[N], sum = 0,k=0;

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            sum += a[j][i];
            if(j==N-1){
                averages_days[k] = sum / N;
                k++;
            }
        }
        sum = 0;
    }

    for (i = 0; i < N; i++) {
        printf("%d\n\n\n\n\n\n\n", averages_days[i]);
        if (averages_days[i] >= max) {
            max = averages_days[i];
            day = i + 1;
        }
    }

    printf("H %d (%d.2) \n", day, max);

    return 0;
}

it's ok now, int day_max_average(int a[M][N]) { int max = 0, day, i, j, averages_days[N], sum = 0,k=0;

for (j = 0; j < N; j++) {
    for (i = 0; i < M; i++) {
        sum += a[i][j];
    } 

    averages_days[k] = sum / M;
    sum = 0;
    k++;
}

for (i = 0; i < N; i++) {
    printf("%d\n", averages_days[i]);
    if (averages_days[i] >= max) {
        max = averages_days[i];
        day = i + 1;
    }
}

printf(" %d  (%d.2) \n", day, max);

return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM