0

I am trying to compute a percentage that calculates the number of home team wins but keep receiving 0 when I know the values I am dividing by are correct.

I've tried calculating it with values that are pointers and then have the percentage outputted by a pointer, but nothing seems to work.

//update function
void update(int * totalGames, int * homeTeamWins, double * homeTeamWinPercentage, int difference) {

*totalGames = *totalGames + 1;
if (difference >= 0) 
    *homeTeamWins = *homeTeamWins + 1;

homeTeamWinPercentage = (*homeTeamWins / *totalGames);

}

//summary function
void summary(int totalGames, int homeTeamWins, double homeTeamWinPercentage) {

//* homeTeamWinPercentage = (homeTeamWins / totalGames);

printf("\n Of the %d total games, %d home teams won, resulting in a %d%.2f%% home team win percentage\n", totalGames, homeTeamWins, homeTeamWinPercentage);

}



// in my main
void main() { 
        ...
        ...
    while (!feof(file1)) {

    output(homeTeam, visitingTeam, difference);
    update(&totalGames, &homeTeamWins, &homeTeamWinPercentage, difference);
}

summary(totalGames, homeTeamWins, homeTeamWinPercentage);
system("pause");
}

Of the 13 total games, 7 home teams won, resulting in a -8589934600.00% home team win percentage.

I can tell it is pointing to somewhere in memory. I have gotten it to print 0.00% for the percentage but that still does not help me.

I am trying to acheive homeTeamWins / totalGames = homeTeamWinPercentage (7/13=53.18%) but am having trouble.

Yeti
  • 1,108
  • 19
  • 28
yayhe
  • 9
  • 1
  • 2
    ^^^ That, also: [while(feof) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Bob__ Oct 04 '19 at 07:48
  • I did a little research myself and found this [What is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) – High Performance Mark Oct 04 '19 at 08:19
  • It would be more sensible, I think, if the `update()` function returned the percentage rather than working via a pointer argument. That's perhaps subjective, but I'd make that argument. – Jonathan Leffler Oct 04 '19 at 09:06
  • Your printf should be generating warnings. Never ignore the warnings until you understand them! – Gem Taylor Oct 04 '19 at 10:56

1 Answers1

5

The problem lies in this line:

homeTeamWinPercentage = (*homeTeamWins / *totalGames);

The expression (*homeTeamWins / *totalGames); will perform integer division and return an integer.

Instead use:

((double) *homeTeamWins / (double) *totalGames)

So, your update function should look like this:

void update(int * totalGames, int * homeTeamWins, double * eTeamWinPercentage,
            int difference)
{
    *totalGames = *totalGames + 1;
    if (difference >= 0) 
        *homeTeamWins = *homeTeamWins + 1;

    *homeTeamWinPercentage = ((double) *homeTeamWins / (double) *totalGames) * 100;
}

P.S: Just noticed you're using very strange way to print the output. Use the following line, just works fine :)

printf("Of %d total Games, %d home teams won, percentage: %0.2f", totalGames, homeTeamWins, percent);

Hope it helps :))

Quanta
  • 594
  • 3
  • 24
  • Output: Of the 13 total games, 7 home teams won, resulting in a -13215283990.00% home team win percentage – yayhe Oct 04 '19 at 07:52
  • That is my output after implementing your "fix" – yayhe Oct 04 '19 at 07:52
  • 1
    Btw, you should probably be multiplying by 100 if you're trying to get percentage :/ – Quanta Oct 04 '19 at 08:03
  • @yayhe I've updated the answer (Tested this time) :) – Quanta Oct 04 '19 at 08:16
  • The alternative fix for the percentage, if you actually only want an integer percentage anyway is to perform the *100 first on the dividand. In fact the quick fix to implicitly get the cast to double is to multiply the dividand by 100.0 – Gem Taylor Oct 04 '19 at 10:55