kokiwebaaa
kokiwebaaa

Reputation: 169

Find the maximum sum of a column of two dimensional arrays in c

I have two dimensional arrays and I want to sum of column, and find the max of sum. I use malloc for array for initialization. When the program finishes I want to print the result, but result is different from what I expect.

it's result->

2 sum is -> 2 max is -> 4201200
5 sum is -> 7 max is -> 4201200
6 sum is -> 13 max is -> 4201200
8 sum is -> 8 max is -> 4201200
9 sum is -> 17 max is -> 4201200
6 sum is -> 23 max is -> 4201200
5 sum is -> 5 max is -> 4201200
9 sum is -> 14 max is -> 4201200
2 sum is -> 16 max is -> 4201200
max is 4201200 column 0

this is my code --->

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

int main() {

    int row = 3, col = 5;
    int *a = (int *)malloc(row * col * sizeof(int));

    int i, j;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
            *(a + i * col + j) = (rand() % (10 - 1 + 1)) + 1;   

    printf("The array elements are:\n");
    
    int sad;
    int max1;
    int sum = 0;
    for (i = 0; i < row; i++) {
        for (j = 0; j < row; j++) {
            printf("%d ", a[j * col + i]);
            sum += a[j * col + i];
            printf("sum is -> %d max is -> %d\n", sum, max1);
        }
        if (sum > max1) {
            printf("max is detected");
            max1 = sum;
            sad = j;
            sum = 0;
        }
        sum = 0;
    }
    printf("max is %d column %d", max1, sad);
    free(a);
    return 0;
}

thank you!

Upvotes: 2

Views: 712

Answers (1)

chqrlie
chqrlie

Reputation: 144780

max1 is unintialized, hence the test if (sum > max1) is meaningless and max1 may not be updated correctly. In your case, max1 happened to have the value 4201200, but the behavior is undefined and this could have been any value or even a trap value on some systems.

Since all matrix elements are positive, you can initialize max1 to 0, otherwise you would use INT_MIN defined in <limits.h> or add a test for the first column.

Furthermore, the index values i and j are swapped in the second loop and the loop test is incorrect too.

Here is a modified version:

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

int main() {

    int rows = 3, cols = 5;
    int *a = (int *)malloc(rows * cols * sizeof(int));

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            a[i * cols + j] = (rand() % (10 - 1 + 1)) + 1;   
        }
    }

    printf("The array elements are:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf(" %2d", a[i * cols + j]);
        }
        printf("\n");
    }
    
    int max_col = 0;
    int max_sum = 0;
    for (int j = 0; j < cols; j++) {
        int sum = 0;
        for (int i = 0; i < rows; i++) {
            sum += a[i * cols + j];
        }
        printf("sum of column %i is -> %d\n", j, sum);
        if (j == 0 || sum > max_sum) {
            printf("max is detected\n");
            max_sum = sum;
            max_col = j;
        }
    }
    printf("max is %d column %d\n", max_col, max_sum);
    free(a);
    return 0;
}

Upvotes: 2

Related Questions