0

Actually not, still I cannot grasp what is wrong with the lock_pairs function in my code.

This is my lock_pairs function:

// Loop to pass each pair
for (int i = 0; i < pair_count; i++)
{

    locked[pairs[i].winner][pairs[i].loser] = true;

    int l = pairs[i].loser;

    int cc = 0;

    int validation = is_winner(pairs[i].loser, candidate_count);

    if (validation == 0)
    {

    }

    else
    {

        locked[pairs[i].winner][pairs[i].loser] = false;
    }

}

// Return void to end function
return;

And this is the recursive function to check if the loser is not a winner on a previously locked pair and if it creates a cycle:

if (cc > 20)
{

    return 1;
}

else
{

     for (int i = 0; i < pair_count; i++)
    {

        if (locked[pairs[i].winner][pairs[i].loser] == 1 && pairs[i].winner == l)
        {

            l = pairs[i].loser;
            cc += 1;
            return is_winner(l, cc);
        }

        else
        {

        }

    }
}

return 0;

I modified a test code to print a grid of integers to show me what was locked after both functions ended. I ran both tests from the class and also 3 other tests I found through forums and the printed grid is always correct (still have to fix my print_winner function thats why I am using a grid to guide me). Still when I run check50 it always give me at least one or two errors on that function.

In the line "if (cc > 20)", if I use a small parameter, like candidate_count or pair_count to stop my recursion from running forever if it encounters a cycle it shows me errors on both ":( lock_pairs locks all pairs when no cycles" and ":( lock_pairs skips middle pair if it creates a cycle". Still if I change it to a bigger number like candidate_count * 2 or a test parameter of 20 like in the code I printed here both errors turn green, still now it shows me an error on ":( lock_pairs skips final pair if it creates cycle" which was previously green.

I am at the verge of throwing my laptop through my window, could someone please give me at least a hint on what might be wrong with my code?

1
  • I wish I had some advice for you. But I'm having a very similar issue. I have run numerous tests that all look successful with the correct winner, but check50 still says: ":( lock_pairs skips final pair if it creates cycle... lock_pairs did not correctly lock all non-cyclical pairs". It's also saying "print_winner did not print winner of election" when it looks to me like it does. But I have debug output that prints the preferences array, sorted pairs list, and locked pairs array, so I can confirm success. I can't replicate the corner case check50 is finding so it does look broken. Any ideas? Jun 1 '21 at 21:02

You must log in to answer this question.

Browse other questions tagged .