hello, i am a C beginner.
please check the code for me.
The program is running fine,but is not giving the correct output or should is say no output. The output supposed to give the code of the team that ranked higher.

#include <stdio.h>
char rankTeams(int, int, int, int, char, int, int, int, int, char);
int getsPoints(int, int);
char highestGoalsFor(int, char, int, char);
char determineHigherGoalDifference(int, int, char, int, int, char);

int main()
{
    char code, code2, highest;
    int won, won2, drawn, drawn2, goalsFor, goalsFor2, goalsAgainst, goalsAgainst2, points, points2, goalDiff;
    printf("Enter the name and statistics for the first team: n ");
    scanf("%s %d %d  %d  %d", &code, &won, &drawn, &goalsFor, &goalsAgainst);
    printf("Enter the name and statistics for the second team: n ");
    scanf("%s %d %d  %d  %d", &code2, &won2, &drawn2, &goalsFor2, &goalsAgainst2);
    rankTeams(won, drawn, goalsFor, goalsAgainst, code, won2, drawn2, goalsFor2, goalsAgainst2, code2);
    printf("Team %c is the highest ranked team. n", highest);
    return 0;
}

char rankTeams(int won, int drawn, int goalsFor, int goalsAgainst, char code, int won2, int drawn2, int goalsFor2, int goalsAgainst2, char code2)
{
    int points, points2, highGD;
    char highest;
    points = getsPoints(won, drawn);
    points2 = getsPoints(won2, drawn2);
    if (points == points2)
    {
        highest = determineHigherGoalDifference(goalsFor, goalsAgainst, code, goalsFor2, goalsAgainst2, code2);
        printf("Team %c is the highest ranked team", highest);
    }
    else if (points > points2 )
        highest = code;
    else
        highest = code2;
    return highest;
}


int getsPoints(int won, int drawn)
{
    int points;
    points = (won * 3) + drawn;
    return points;
}


char highestGoalsFor(int goalsFor, char code, int goalsFor2, char code2)
{
    char highest;
    if (goalsFor > goalsFor2)
        highest = code;
    else if (goalsFor < goalsFor2)
        highest = code2;    
    else
        printf("Team %c is ranked the same as team %c. n", code2, code);
    return highest;
}


char determineHigherGoalDifference(int goalsFor, int goalsAgainst, char code, int goalsFor2, int goalsAgainst2, char code2)
{
    int goalDiff;
    char highGD;
    goalDiff = (goalsFor - goalsAgainst) - (goalsFor2 - goalsAgainst2);

    if (goalDiff == 0)
        highGD = highestGoalsFor(goalsFor, code, goalsFor2, code2);
    else if (goalDiff > 0)
        highGD = code;
    else
        highGD = code2;
    return highGD;
}

Edited 3 Years Ago by Dani: Formatting fixed

Attachments

Your using a string format specifier %s for a character %c. Line 12 and 14.

Edited 5 Years Ago by gerard4143: n/a

Is it always the whitespace character that is shown in place of the Team code??

(It would have been easier for me if you had used comments).

Anyway, here's what i gather from the above program:

Everything is fine until line 15. From here the control passes to the rankTeams function. When the control is inside the function the problem starts from line 26. Here you have a bunch of if conditions. If the first condition satisfies the text "Team %c is the highest ranked team"(with the desired character in place of %c) is printed twice; once inside the rankTeams()and again inside main(). The first time its printed you'll see the desired output. So for some kinds of input (when the condition on line 26 satisfies) you are gonna get the correct output.

You won't get the desired output in other cases because the variable highest in main has garbage value in it. The function rankTeams() also has a variable called highest. Please note that the changes made to the highest inside the rankTeams() function would not effect the value of the highest in the main() function because the memory locations of both the variables are different.

One solution to this is to assign the value of highest in main() the value returned from the function rankTeams(). So, line 15 would then become:

highest = rankTeams(won, drawn, goalsFor, goalsAgainst, code, won2, drawn2, goalsFor2, goalsAgainst2, code2);

Also then line 28 can be removed from the program.

Happy Programming!!

This article has been dead for over six months. Start a new discussion instead.