Task is to find the column number(index) that have maximum sum of it's elements | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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,max =0; cin >> n >> m; int aza[100][100]; for (int i = 1; i <= n; i++) { for (int j = 1;j <= m; j++) { cin >> aza[i][j]; } } for (int j = 1; j <= m; j++) { int sum = 0; for (int i = 1; i <= n; i++) { sum+= aza[i][j]; } } int maxind = 0; for (int j = 1; j <= m; j++) { for (int i = 1; i <= n; i++) { if (max < aza[i][j]) { max = aza[i][j]; maxind = i; } } } cout << maxind; }

4th Oct 2020, 8:00 AM
Azat Malgazhdar
Azat Malgazhdar - avatar
1 Answer
+ 2
Azat Malgazhdar I see the code finds the sum of each column but then does nothing with that sum. It is missing logic that tracks and reports which column index has the largest sum. The logic would look similar to the if statement seen in the final nested loops that find the column with the highest value. Although, the sum comparison should be placed differently. It should go after the inner loop has completed, so that its total sum is compared with maxsum.
4th Oct 2020, 8:37 AM
Brian
Brian - avatar