The program is used to display the winning team which score the highest score within 3 teams in a 3x4 array.
There is something wrong in the input or teamscore function. I can't find the errors and my input in the input function can't be stored and calculated in teamscore funtion.
void input(int a[NROW][NCOL])
{
int x,y;
double team;
for(x=0;x<NROW;x++)
{
printf("\nEnter your team: ");
scanf("%i",&team);
for(y=0;y<NCOL;y++)
{
printf("\nEnter the score: ");
scanf("%lf",&a[x][y]);
}
}
}
int teamscore(int a[NROW][NCOL])
{
int x,y,highest,team;
double sum;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
sum = sum + a[x][y];
}
}
for(x=0;x<NROW;x++)
for(y=0;y<NCOL;y++)
if (a[x][y]>highest)
{
highest=a[x][y];
}
return highest;
}
All of my output is 0.
This is my entire code.
#include <stdio.h>
#define NROW 3
#define NCOL 4
void initialize(int a[NROW][NCOL])
{
int x,y;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
a[x][y]=0;
}
}
}
/* Display array in a matrix form*/
void disp_arr(int a[NROW][NCOL])
{
int x,y;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
printf("%i ",a[x][y]);
}
printf("\n");
}
}
/* Input the invidual score for 3 team*/
void input(int a[NROW][NCOL])
{
int x,y;
double team;
for(x=0;x<NROW;x++)
{
printf("\nEnter your team: ");
scanf("%i",&team);
for(y=0;y<NCOL;y++)
{
printf("\nEnter the score: ");
scanf("%lf",&a[x][y]);
}
}
}
/* Calculate the total of score for each team and return the index for the
row with the highest team score */
int teamscore(int a[NROW][NCOL])
{
int x,y,highest,team;
double sum;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
sum = sum + a[x][y];
}
}
for(x=0;x<NROW;x++)
for(y=0;y<NCOL;y++)
if (a[x][y]>highest)
{
highest=a[x][y];
}
return highest;
}
int main()
{
int ar[NROW][NCOL];
int team;
initialize(ar);
disp_arr(ar);
input(ar);
disp_arr(ar);
team=teamscore(ar);
printf("\nThe winniing team is Team %i",&team);
return 0;
}
I would appreciate for some helps.
Thanks!
teamscore, you do not initializesumto zero, but you should. Similarly, you don't initializehighest, though that can't necessarily be set to zero safely; you usually set it to the first (or last) value in the array and compare other values against that. Also, don't forget to include a newline at the end of lines of output.input(), you have:double team; for (x = 0; x < NROW; x++) { printf("\nEnter your team: "); scanf("%i", &team);— but that's a type mismatch. Your compiler should be complaining. If it isn't, either turn up the warning levels or get a better compiler. Do not pass&itoprintf(). It's not clear why you calculatesumsince you never use it.scanf("%lf", &a[x][y]);—ais an array ofint, notdouble.int getInt(int defaultValue) { char ar[32]; if (fgets(ar,sizeof(ar),stdin)!=NULL) { return atoi(ar); } else { return defaultValue; } }