2D array Find the maximum sum

I want to find the highest scored player and print it.Can anyone explain the logic that i can use in
int Findwinner(int data[][4],int rowSize,int colSize)
thanks..
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
41
42
43
#include<iostream>
using namespace std;
void inputdata(int data[][4],int rowSize,int colSize);
int Findwinner(int data[][4],int rowSize,int colSize);

int main()
{
int tbl[3][4];
inputdata(tbl,3,4);
Findwinner(tbl,3,4);
return 0;
}


void inputdata(int data[][4],int rowSize,int colSize)
{
 for(int a=0;a<colSize;a++)
  {
    cout<<"Round "<<a+1<<endl;
    for(int b=0;b<rowSize;b++)
        {
            cout<<"Enter score of player 100"<<b+1<<":"<<endl;
            cin>>data[b][a];
         }
   }
}


int  Findwinner(int data[][4],int rowSize,int colSize)
{
int sum;
  for(int a=0;a<colSize;a++)
  {
    sum=0;
        for(int b=0;b<rowSize;b++)
        {

      sum=sum+data[b][a];
         }
cout<<"Sum"<<sum<<endl;

  }
}
just set the maximum to the first element then iterate over them and if it is greater then assign that to the maximum. If you wish to keep track of position have a second variable for that too. I would normally avoid checking the same element twice but in this case you have a 2d array so not as easy as starting at 1 instead of 0 like in a 1d.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int max = array[0][0];
unsigned posRow = 0;
unsigned posColumn = 0;
for(int row = 0; row < rows; ++row)
{
    for(int column = 0; column < columns; ++column)
    {
        if(array[row][column] > max)
        {
            max       = array[row][column];
            posRow    = row;
            posColumn = column;
        }
    }
}
Last edited on
Topic archived. No new replies allowed.