Unsure How to Declare Winner in Array After Sorting
Apr 12, 2017 at 9:18pm UTC
Hello, thank you in advance for any help. I have a code that asks for the scores between two teams stored inside a double array. After I find the scores I need to find the difference between each score, and sum the differences per team. Whichever team has the largest sum is the winner, and I have a code that works, but I do not know how to declare the winner of the game.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
using namespace std;
const int SIZE = 4;
void det_win(int diff[][4], int SIZE);
int main()
{
int scores[4][4], diff[4][4];
int i , j;
for (i=0; i < 4; i++)
{
for (j=0; j < 4; j++)
{
if (i != j)
{
cout << "Please enter the score of team " << i+1 << " versus team " << j+1 << "." << endl;
cin >> scores[i][j];
}
}
}
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
diff[i][j] = scores[i][j] - scores[j][i];
}
}
det_win(diff, SIZE);
return 0;
}
void det_win(int diff[][4], int SIZE)
{
int i, j = 0, k;
int sum_row[4] = {0,0,0,0};
for (j=0; j<4; j++)
{
i=0;
sum_row[i] += diff[i][j];
}
for (j=0; j<4; j++)
{
i=1;
sum_row[i] += diff[i][j];
}
for (j=0; j<4; j++)
{
i=2;
sum_row[i] += diff[i][j];
}
for (j=0; j<4; j++)
{
i=3;
sum_row[i] += diff[i][j];
}
int temp;
for (i=3; i>0; i--)
{
for (k=0; k<i; k++)
{
if (sum_row[k] < sum_row[k+1])
{
temp=sum_row[k+1];
sum_row[k+1]=sum_row[k];
sum_row[k]=temp;
}
}
}
cout << "Team " << ___ << " is the winner! They won with a score of " << temp << "." << endl;
return ;
}
Last edited on Apr 12, 2017 at 9:19pm UTC
Apr 12, 2017 at 10:16pm UTC
I ended up using a different sorting mechanism to find the answer, and this is the code I ended up going with that worked if anyone comes across this lost!
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
using namespace std;
const int SIZE = 4;
void det_win(int diff[][4], int SIZE);
int main()
{
int scores[4][4], diff[4][4];
int i , j;
for (i=0; i < 4; i++)
{
for (j=0; j < 4; j++)
{
if (i != j)
{
cout << "Please enter the score of team " << i+1 << " versus team " << j+1 << "." << endl;
cin >> scores[i][j];
}
}
}
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
diff[i][j] = scores[i][j] - scores[j][i];
}
}
det_win(diff, SIZE);
return 0;
}
void det_win(int diff[][4], int SIZE)
{
int i, j=0, k;
int sum_row[4] = {0,0,0,0};
for (j=0; j<4; j++)
{
i=0;
sum_row[i] += diff[i][j];
}
for (j=0; j<4; j++)
{
i=1;
sum_row[i] += diff[i][j];
}
for (j=0; j<4; j++)
{
i=2;
sum_row[i] += diff[i][j];
}
for (j=0; j<4; j++)
{
i=3;
sum_row[i] += diff[i][j];
}
int max = 0;
max=sum_row[0];
int maxindex = 0;
for (i=0; i<4; i++)
{
if (max<sum_row[i])
{
max=sum_row[i];
maxindex=i;
}
}
cout << "Team " << maxindex+1 << " is the winner! They won with a score of " << max << "." << endl;
return ;
}
Topic archived. No new replies allowed.