2D array find winner and print it..

I want to find the winner and print the winner's ID.Here i have tried to get the maximum sum But it doesn't outputs the maximum sum.
can anyone show me how can get the required outputs.

And also in my question I have to use data[][5].And I have to store the scores in 4x3 2D array.But I used 4x5 2D array in the main program otherwise it gives an error message.


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
45
46
47
48
49
50
51
52
 #include<iostream>
using namespace std ;
void inputdata(int data[][5],int colSize,int roSize);
void Findwinner(int data[][5],int colSize,int roSize);
int main()
{

int tbl[4][3];

inputdata(tbl,4,5);
Findwinner(tbl,4,5);


return 0;
}

void inputdata(int data[][5],int rowSize,int colSize)
{

for(int a=1;a<colSize;a++)
{
cout<<"Enter round "<<a<<endl;

  for(int b=1;b<rowSize;b++)
   {
      cout<<"   Enter score of Player 10"<<b<<":";
      cin>>data[a][b];
   }
}

}

void Findwinner(int data[][5],int rowSize,int colSize)
{
int sum;
int max=data[1][1]+data[1][2]+data[1][3];

                for(int a=2;a<rowSize;a++)
                {
                   sum =0;
                     for(int b=1;b<colSize;b++)
                        {
                          sum =sum+data[a][b];
                       }
                           if(max<sum)
                       {
                             max=sum;
                     }
     }
 cout<<"Maximum sum is "<<sum<<endl;
}
 
You will probably find most of your problems relate to you starting the iteration on the second element in both dimensions of your 2d arrays.

when you declare an array[3]
your array elements are [0][1][2] not [1][2][3]

so in the code
1
2
3
4
5
6
7
8
9
10
for(int a=1;a<colSize;a++)
{
cout<<"Enter round "<<a<<endl;

  for(int b=1;b<rowSize;b++)
   {
      cout<<"   Enter score of Player 10"<<b<<":";
      cin>>data[a][b];
   }
}
and various other places. you are either starting or adding int max=data[1][1]+data[1][2]+data[1][3]; from the second element/s.

Make sure you do not access or write past arrays this can corrupt data or crash your program.
Last edited on
The other problem you're going to have is that your array bounds and the size of your table do not agree.

At line 8, you've declared tbl to be 4x3.
At lines 10 and 11, you telling your functions the table is 4x5. That's going to cause you to index past the end of the table.

It's good practive to define constant values for array bounds and then use those as your loop limits. This makes it easy to change the size of the array in one place and not have to make many changes throughout your program.

1
2
3
4
5
6
7
const int max_rows = 4;
const int max_cols = 5; 
...
  int tbl[max_rows][max_cols];
...
  inputdata (tbl,max_rows,max_cols);
  Findwinner (tbl,max_rows,max_cols);




Topic archived. No new replies allowed.