Search code examples
ccs50

cs50 pset 3, printing winner function not working


// Print the winner of the election, if there is one
bool print_winner(void)
{
    // TODO
    float votes2win;
    //Votes could be odd number so would need to be rounded
    int rounded_votes;

    votes2win = voter_count / 2;
    rounded_votes = round(votes2win);

    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > rounded_votes)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
    }
    return false;
}

need to print winner cant seem to find the problem in this function is it the function or something else in my code?


Solution

  • In a runoff election with 3 voters, this function will only print candidates who have 3 votes because of the use of round. Exploit the fact that C truncates integer division.