2D array Print the winner.

I want to print the highest scored player..
How can I modify this program for get that output.?
the Player numbers are 100,101,102,103 .
Here im getting Maximum sum of each player.But it also gives the correct maximum sum if the maximun sum come from last column (last set of marks)..

now Im getting output like this

Round 1
Enter score of Player 100:2
Enter score of Player 101:2
Enter score of Player 102:2
Enter score of Player 103:2
Round 2
Enter score of Player 100:3
Enter score of Player 101:3
Enter score of Player 102:3
Enter score of Player 103:3
Round 3
Enter score of Player 100:4
Enter score of Player 101:4
Enter score of Player 102:4
Enter score of Player 103:4
Winner is 16


Can anyone help me to fix these problems ?
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
44
#include<iostream>
using namespace std;
void inputData(int data[][3],int rowSize,int colSize);

int FindWinner(int data[][3],int rowSize,int colSize);
int main()
{
int tbl  [4][3];
inputData(tbl,3,4);
FindWinner(tbl,3,4);
return 0;
}

void inputData(int data[][3],int rowSize,int colSize)
{
        for(int a=0;a<rowSize;a++)
        {
          cout<<"Round "<<a+1<<endl;
          for(int b=0;b<colSize;b++)
           {
             cout<<"   Enter score of Player 10"<<b<<":" ;
             cin>>data[a][b];
           }
       }
}

int  FindWinner (int data[][3],int rowSize,int colSize)
{
int maxsum;

int sum ;
                for(int a=0;a<rowSize;a++)
                {sum =0;
                  for(int b=0;b<colSize;b++)
                    {
                         sum=sum + data[a][b];
                     }
                       if(sum>maxsum)
                   maxsum =sum;

                 }
 cout<<"Winner is "<<maxsum<<endl;
}


Last edited on
So this is your table of 3 rounds and 4 players.

array[row][col]

tbl[3][4]

	player(b=4)
	0 1 2 3
r	---------
o 0  |	2 2 2 2
u 1  |	3 3 3 3
n 2  |	4 4 4 4
d
(a=3)


To find the winner, for each player, you want to add their score in each of the three rounds. (Adding down each column). So the outer loop of the for would be the b value, the inner loop would be the a value - right now you have the a in the outside loop, so you're getting the total of the scores for each round. (Adding across each row)

Edit: In addition to tracking the max score, you might also want to keep track of the index of the player associated with the max score. Also - what if there's a tie?

line 38 - maxsum isn't initialized when you try to compare it here
Last edited on
Topic archived. No new replies allowed.