the location of the maximum sum in a column

I need to add the columns and print which column had the maximum sum and the sum of that column.

So I already added the columns and got down the max, but I can't figure how to print the column as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <stdio.h>

int main()
{
	int matrix[2][2], i, j, sum=0, max=0, x=1, y=1, locaa=0;
	
	for(i=0; i<2 ;i++, x++)
	{
		for(j=0; j<2 ;j++, y++)
		{
			printf("\nEnter matrix[%d][%d] value: ", x, y);
			scanf("%d", &matrix[i][j]);
		}
		y=1;
	}
	printf("\n");
	for(j=0; j<2 ;j++)
	{
		for(i=0; i<2 ;i++)
		{
			sum=sum+matrix[i][j];
		}
		printf("Sum is: %d\n", sum);
		if(sum>max)
		{
			max=sum;
			locaa=j;
		}
		sum = 0;
	}
	printf("Column #%d has a maximum sum of %d. \n", j, max);
}
Why C and not C++ for i/o?
Why declare temporary values for indexing in advance? (i,j...) They should live and die in respective for loops, because you shouldn't need them afterwards.
Why 1-based indices and not 0-based? a 2x2 matrix should have values [0][0], [0][1], [1][0], [1][1], so it's pretty confusing when you're asking user to input value for cell [1][2].

Otherwise the logic looks okay-ish (might want a max_column variable to not depend on j). If you used better variable names, the answer would be clearer:
-- "row" instead of "i"
-- "col" instead of "j"

To get the column sums, you found (perhaps by chance?) to iterate over column before row.
line 23 --> printf("Sum of column #%d is: %d\n", j, sum);
Last edited on
Here's fuller example of what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int main()
{
    const int ROWS = 3;
    const int COLS = 3;
    int matrix[ROWS][COLS];
    for(int row=0; row<ROWS; ++row)
    {
        for(int col=0; col<COLS; ++col)
        {
            cout << "Enter matrix[" << row << "][" << col <<"] value: ";
            cin >> matrix[row][col];
        }        
    }
    cout << '\n';

    // Calculate column sums
    int sum, max_sum=0, max_col=0;
    for(int col=0; col<COLS; ++col)
    {
        sum = 0;        
        for(int row=0; row<ROWS; ++row)
        {
            sum += matrix[row][col];
        }
        cout << "Sum of column #" << col << " is " << sum << '\n';
        if (sum > max_sum)
        {
            max_sum = sum;
            max_col = col;
        }
    }

    cout << "Column #" << max_col << " has a maximum sum of " << max_sum << '\n';

    return 0;
}


example input/output:
Enter matrix[0][0] value:  1
Enter matrix[0][1] value:  2
Enter matrix[0][2] value:  3
Enter matrix[1][0] value:  4
Enter matrix[1][1] value:  6
Enter matrix[1][2] value:  5
Enter matrix[2][0] value:  7
Enter matrix[2][1] value:  9
Enter matrix[2][2] value:  8

Sum of column #0 is 12
Sum of column #1 is 17
Sum of column #2 is 16
Column #1 has a maximum sum of 17
So far we are using c in our classes. We haven't reached c++ yet, that's around the next semester. Our prof. wants [1,1], [1,2], [2,1], [2,2] kinda values.

And I forgot to remove line 23. It was only there as helping reference for the first few parts.
So it should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

int main()
{
	int matrix[2][2], i, j, sum=0, max=0, x=1, y=1, locaa=0;
	
	for(i=0; i<2 ;i++, x++)
	{
		for(j=0; j<2 ;j++, y++)
		{
			printf("\nEnter matrix[%d][%d] value: ", x, y);
			scanf("%d", &matrix[i][j]);
		}
		y=1;
	}
	printf("\n");
	for(j=0; j<2 ;j++)
	{
		for(i=0; i<2 ;i++)
		{
			sum=sum+matrix[i][j];
		}
		if(sum>max)
		{
			max=sum;
			locaa=j;
		}
		sum = 0;
	}
	printf("Column #%d has a maximum sum of %d. \n", locaa, max); //trouble with location 


As mentioned before, I have trouble on the last parts.
Okay, thanks for responses. Internally you should probably still do all the math and indexing with 0-based, since it's more natural. Just when you output messages to user, add 1. For example, printf("%d", row+1);

For remembering the best column --> see how I used the max_col variable. Remember this column as well as max sum.
Here is C version with 1-based indexing shown to user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>

int main()
{
    const int ROWS = 2;
    const int COLS = 2;
    int matrix[ROWS][COLS];    
    for(int row=0; row<ROWS; ++row)
    {
        for(int col=0; col<COLS; ++col)
        {
            printf("Enter matrix[%d][%d] value: ", row+1, col+1);
            scanf("%d", &matrix[row][col]);
        }
    }
    printf("\n");

    // Calculate column sums
    int sum, max_sum=0, max_col=0;
    for(int col=0; col<COLS; ++col)
    {
        sum = 0;        
        for(int row=0; row<ROWS; ++row)
        {
            sum += matrix[row][col];
        }
        printf("Sum of column #%d is %d\n", col+1, sum);
        if (sum > max_sum)
        {
            max_sum = sum;
            max_col = col;
        }
    }
    printf("Column #%d has a maximum sum of %d. \n", max_col+1, max_sum); 

    return 0;
}


sample input/output
Enter matrix[1][1] value:  4
Enter matrix[1][2] value:  3
Enter matrix[2][1] value:  2
Enter matrix[2][2] value:  1

Sum of column #1 is 6
Sum of column #2 is 4
Column #1 has a maximum sum of 6.
Topic archived. No new replies allowed.