0

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!

6
  • 1
    In teamscore, you do not initialize sum to zero, but you should. Similarly, you don't initialize highest, 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. Commented Dec 1, 2017 at 5:01
  • 2
    Also, in 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 &i to printf(). It's not clear why you calculate sum since you never use it. Commented Dec 1, 2017 at 5:06
  • 1
    And you're using the wrong format in scanf("%lf", &a[x][y]);a is an array of int, not double. Commented Dec 1, 2017 at 5:11
  • when you read from the keyboard: do a function that uses fgets instead, then convert the result to type you need. it is much easier and safer to handle than scanf's funny formatting and buffer. e.g. int getInt(int defaultValue) { char ar[32]; if (fgets(ar,sizeof(ar),stdin)!=NULL) { return atoi(ar); } else { return defaultValue; } } Commented Dec 1, 2017 at 6:41
  • Thanks @JonathanLeffler for pinpoint the mistakes that i made Commented Dec 1, 2017 at 18:55

2 Answers 2

2

You code is ambiguous as to what you intend the team to be. You prompt for user input and then attempt to store team as double? Unless you expect your team number to be, e.g. 123.456, just make your team an int, e.g. team 1, team 10, etc..

The next problem you have is taking input for a team number, but providing no manner for that entry to be usable in any other function other than input. If you don't pass another parameter to capture the user input for team, then your teams are just the indexes for a and it is unnecessary to prompt for that input. To make the user input available back in main, just pass another parameter (or just add +1 to NCOL and use col 0 for the team number). The following is a way to capture the user input for the team number (e.g. team 125, 190, ...) and make that available back in the caller:

int main (void) {

    int hi_index = 0,
        teams[NROW] = {0},      /* note array of NROW for teams */
        ar[NROW][NCOL] = {{0}};
    ...
    input (ar, teams);

then you can make input capture that information by passing the teams array as a parameter, e.g.

/* Input the individual score for 3 team*/
void input (int a[NROW][NCOL], int teams[NROW])

You must validate all user input. When working with scanf that means validating the return -- that the number of conversions (as specified in the format string) did in fact take place. You must also check whether the user generated a manual EOF (Ctrl+d or Ctrl+z on windoze). If the user cancels input, your code should gracefully handle that condition. If the user just provided invalid input and the conversion failed due to a matching or input failure, you need to determine if you will inform the user and prompt the user to "Try Again" or whether you will treat it as a cancellation.

To approach user input in this manner, one method is just to loop until you receive valid input or the user cancels, e.g. in your input function, you could do the following:

        for (;;) {      /* loop until valid input or user canceled */
            int rtn;

            printf ("\nEnter your team: ");
            rtn = scanf ("%d", &teams[x]);

            if (rtn == 1)
                break;
            else if (rtn == EOF) {
                fprintf (stderr, "user canceled input.\n");
                exit (EXIT_FAILURE);
            }
            fprintf (stderr, "error: invalid input.\n");
            /* (you should empty stdin here) */
            empty_stdin();
        }

note: to empty stdin, you can simply read with getchar() until '\n' or EOF is encountered, e.g.

/* function to empty stdin */
void empty_stdin()
{
    int c = getchar();

    while (c != '\n' && c != EOF)
        c = getchar();
}

Your printf and scanf format specifier mismatches are already covered in the comments.

Putting all those pieces together, and presuming you actually want whole numbers as the team numbers instead of weird team numbers like 123.456, you could do something similar to the following:

#include <stdio.h>
#include <stdlib.h> /* for exit, EXIT_FAILURE */

#define NROW 3
#define NCOL 4

/* Display array in a matrix form*/
void disp_arr (int a[NROW][NCOL], int teams[NROW])
{
    int x, y;

    putchar ('\n');
    for ( x = 0; x < NROW; x++)
    {
        printf ("team %3d : ", teams[x]);
        for (y = 0; y < NCOL; y++)
            printf (" %4d", a[x][y]);
        putchar ('\n');
    }
}

/* function to empty stdin */
void empty_stdin()
{
    int c = getchar();

    while (c != '\n' && c != EOF)
        c = getchar();
}

/* Input the individual score for 3 team*/
void input (int a[NROW][NCOL], int teams[NROW])
{
    int x, y;

    for (x = 0; x < NROW; x++)
    {
        for (;;) {      /* loop until valid input or user canceled */
            int rtn;

            printf ("\nEnter your team: ");
            rtn = scanf ("%d", &teams[x]);

            if (rtn == 1)
                break;
            else if (rtn == EOF) {
                fprintf (stderr, "user canceled input.\n");
                exit (EXIT_FAILURE);
            }
            fprintf (stderr, "error: invalid input.\n");
            empty_stdin();
        }
        for (y = 0; y < NCOL; y++)
        {
            for (;;) {  /* same loop until valid or canceled */
                int rtn;

                printf ("Enter team[%d] score[%d]: ", teams[x], y + 1);
                rtn = scanf ("%d", &a[x][y]);

                if (rtn == 1)
                    break;
                else if (rtn == EOF) {
                    fprintf (stderr, "user canceled input.\n");
                    exit (EXIT_FAILURE);
                }
                fprintf (stderr, "error: invalid input.\n");
                empty_stdin();
            }
        }
    }
}

/* Calculate the total of score for each team.
* return the index for the row with the highest team score 
*/
int teamscore (int a[NROW][NCOL])
{
    int x, y, 
        highest = 0,
        hi_index = 0, 
        sum[NROW] = {0};

    for (x = 0; x < NROW; x++) {
        for (y = 0; y < NCOL; y++)
            sum[x] += a[x][y];
        if (sum[x] > highest) {
            highest = sum[x];
            hi_index = x;
        }
    }

    return hi_index;
}

int main (void) {

    int hi_index = 0,
        teams[NROW] = {0},
        ar[NROW][NCOL] = {{0}};

    // initialize (ar); /* ar is not a VLA, initialize at declaration */

    disp_arr (ar, teams);

    input (ar, teams);

    disp_arr (ar, teams);

    hi_index = teamscore (ar);

    printf ("\nThe winning team is Team %d\n", teams[hi_index]);

    return 0;
}

Example Use/Output

$ ./bin/team

team   0 :     0    0    0    0
team   0 :     0    0    0    0
team   0 :     0    0    0    0

Enter your team: 123
Enter team[123] score[1]: 10
Enter team[123] score[2]: 12
Enter team[123] score[3]: 14
Enter team[123] score[4]: 16

Enter your team: 138
Enter team[138] score[1]: 12
Enter team[138] score[2]: 14
Enter team[138] score[3]: 16
Enter team[138] score[4]: 18

Enter your team: 187
Enter team[187] score[1]: 11
Enter team[187] score[2]: 13
Enter team[187] score[3]: 15
Enter team[187] score[4]: 17

team 123 :    10   12   14   16
team 138 :    12   14   16   18
team 187 :    11   13   15   17

The winning team is Team 138

Example of Invalid & Canceled Input

$ ./bin/team

team   0 :     0    0    0    0
team   0 :     0    0    0    0
team   0 :     0    0    0    0

Enter your team: The Mighty Ducks
error: invalid input.

Enter your team: user canceled input.

Look things over and let me know if I made any error in my presumptions about the type for your teams etc. Let me know if you have further questions.

Sign up to request clarification or add additional context in comments.

1 Comment

The type for my team should be integer so your presumptions are correct. Well explained and i need to take some times to understand it. Thanks for helping.
0
  1. use %d for integer instead of %lf. %lf for double 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("%d",&a[x][y]);// use %d for integer instead of %lf. %lf for double
        }
    
    }
    

    }

  2. Lots of mistake in teamscore function. Sample code:

    int teamscore(int a[NROW][NCOL])
    {
    int x,y,highest,team;
    double sum;
    
    for(x=0;x<NROW;x++)
    {
        sum =0;
        for(y=0;y<NCOL;y++)
        {
            sum = sum + a[x][y];
        }
        a[x][0] = sum;
    }
    highest = a[0][0];
    team = 0;
    
    for(x=0;x<NROW;x++)
        if (a[x][0]>highest)
        {
            highest = a[x][0];
            team=x;
        }
    return team;// return row number
    

    }

  3. use team variable only instead of &team in main while printing. &team = Address of the team variable.

    printf("\nThe winniing team is Team %d",team);
    

2 Comments

You may want to discuss what team is and how it is not "the index for the row with the highest team score".
Now i know i have done a lot of mistakes.Thanks for the assists and the final output is incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.