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);
#include <iostream>
usingnamespace std;
int main()
{
constint ROWS = 3;
constint 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:
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.
#include <stdio.h>
int main()
{
constint ROWS = 2;
constint 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.