0

I tried the following idea to print winner function. Can you check why it is not working?

  1. Defined a function named find_majority. This is to get around the float problem.

  2. called the function and assigned the value to the majority.

  3. Use this variable in the print winner function

  4. Find majority function

' int find_majority( voter_count) { int winning_votes; if(voter_count%2==0) { winning_votes = voter_count / 2; return winning_votes;

}
else
{
    int temp_winning_votes= voter_count + 1;
    winning_votes = temp_winning_votes/2;
    return winning_votes;
}

} '

  1. Calling the function

'majority = find_majority();'

  1. using the variable

' // Print the winner of the election, if there is one bool print_winner(void) {

for (int i=0; i<candidate_count; i++)
{
    if(candidates[i].votes>majority)
        
        {

            printf("%s\n",candidates[i].name);
            return true;

        }
    
}

return false;

} '

1 Answer 1

0

You don't say what is "not working" about it. But maybe this conditional if(candidates[i].votes>majority) should be >=.

Be forewarned: This program will not pass check50. Assuming plurality, from the spec:

You should not modify anything else in plurality.c other than the implementations of the vote and print_winner functions (and the inclusion of additional header files, if you’d like).

The same warning applies to all the pset3 exercises.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .