0

Task is to find the column number(index) that have maximum sum of it's elements

/*Input First line N and M (1<=N,M<=100). Then NxM table is given(all number are integers) Output The index of the column with maximum sum of it's elements. Sample input: 3 4 1 2 3 1 1 5 6 1 1 1 8 1 Sample output: 3 What is wrong ?*/ #include <iostream> using namespace std; int main() { int n, m, i, j, counter = 0; int aza[100][100], max[100], temp; cin >> n >> m; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> aza[i][j]; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (aza[i][j] > 0) { counter++; aza[i][j] = max[i]; } } } cout << counter << endl; temp = 0; for (i = 0; i < counter; i++) { for (j = i + 1; j < counter; j++) { if (max[i] > max[j]); { temp == max[i]; max[i] = max[j]; max[j] = temp; } } cout << max[i] << " "; } return 0; }

3rd Oct 2020, 11:34 AM
Azat Malgazhdar
Azat Malgazhdar - avatar
1 Answer
0
is this from hackerrank or what? what's the purpose of : aza[i] [i] = max[i]; when max[] is not initialized. temp == max[i] //not an assignment. the explanation does not make any sense. index of column with max sum of elements is not 3 in the example unless indices start from 1. it should be 2. *you are not calculating the sum of the columns elements . if I understand : 3 4 1 2 3 1 1 5 6 1 1 1 8 1 column with max sum is at index 2 3+6+8 = 17 in your code you didn't sum any column.
3rd Oct 2020, 1:28 PM
Bahha┣
Bahha┣ - avatar