0

I have implemented everything successfully up until the 'Print Winner Function', and although I'm pretty sure my logic is correct, and doing manual tests myself yield the correct winner's name.

The checking algorithm says:

:( print_winner prints winner of election when one candidate wins over all others

print_winner did not print winner of election

:( print_winner prints winner of election when some pairs are tied

print_winner did not print winner of election

My code:

void print_winner(void)
{
    // Goes through all pairs
    for (int i = 0; i < pair_count; i++)
    {
        // Checks if a pair is locked and the winner has never lost by calling 'isNeverLost'
        if (locked[pairs[i].winner][pairs[i].loser] && isNeverLost(pairs[i].winner))
        {
            // Prints the name of the winner who never lost
            printf("%s", candidates[pairs[i].winner]);
            return;
        }
    }
}

// Check if a winner has lost before
bool isNeverLost(int winner)
{   // Goes through all pairs and checks if the winner of the main loop pair, has lost in a locked 
    // pair before
    for (int i = 0; i < pair_count; i++)
    {
        if (locked[pairs[i].winner][winner])
        {   
            // If a winner lost (aka has an arrow pointing at them) 
            return false;
        }
    }
    // Else if never lost before
    return true;
}

I'm not sure what's wrong with my code, help would be much appreciated, but please no code answers. If you'd be kind enough to just point out what I'm doing wrong, and what I'd might consider, that would be amazing. Thank you.

solved

0

Checking for each candidate first in the main loop, then comparing against locked winners solved it for me.

You must log in to answer this question.

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