I've tested my code several times and it works perfectly fine. I get the correct winners, I even get multiple winners without any issue. But check50 still gives me the following results:
:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:( print_winner identifies Alice as winner of election
print_winner function did not print winner of election
:( print_winner identifies Bob as winner of election
print_winner function did not print winner of election
:( print_winner identifies Charlie as winner of election
print_winner function did not print winner of election
:( print_winner prints multiple winners in case of tie
print_winner function did not print both winners of election
:( print_winner prints all names when all candidates are tied
print_winner function did not print all three winners of election
my printing code:
// Print the winner (or winners) of the election
void print_winner(void)
{
bool n = 0;
for (int i = candidate_count; i > 0; i--)
{
for (int j = 0; j < candidate_count; j )
{
if (candidates[j].votes == i)
{
printf("%s\n", candidates[j].name);
n = true;
}
}
if (n == true)
{
break;
}
}
}
CodePudding user response:
found the issue, although don't understand why it's this line:
for (int i = candidate_count; i > 0; i--)
it should be
for (int i = MAX; i > 0; i--)
candidate_count 1 didn't work as well, it should be MAX for some reason
CodePudding user response:
If there are 3 candidates and one gets 6 votes, your "count down and count up" will never find that winner...
int winner = 0;
for( int i = 1; i < ncandidates; i )
if( candidates[ i ].votes > candidates[ winner ].votes )
winner = i;
for( int i = 0; i < ncandidates; i )
if( candidates[ i ].votes == candidates[ winner ].votes )
printf( "Winner: %s\n", candidates[ i ].votes );
Now, your exercise is to distinguish "multiple winners" (ie: a tie)...