0

I've implemented a recursive function to check for cycles and return a boolean based on if a cycle is found or not. Check50 only returns one error, and that is for "lock_pairs skips final pair if it creates cycle". The middle pair checks seem to work fine, and I'm wondering if there is a logical error in my code that would cause the final pair to get incorrectly added. If anyone has any insight, I would very much appreciate hearing some feedback. Thanks!

void lock_pairs(void)
{
    pair locked_pairs[pair_count];
    int locked_pair_count = 0;
    for (int i = 0; i < pair_count; i ++)
    {
        if (!cycle_check(pairs[i].winner, pairs[i].loser, locked_pairs, locked_pair_count))
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
            locked_pairs[locked_pair_count] = pairs[i];
            locked_pair_count ++;
        }
    }
}

bool cycle_check(int check_against, int current_cand, pair locked_pairs[], int locked_pair_count)
{
    if (current_cand == check_against)
    {
        return true;
    }

    for (int i = 0; i < locked_pair_count; i ++)
    {
        if (locked_pairs[i].winner == current_cand)
        {
            return cycle_check(check_against, locked_pairs[i].loser, locked_pairs, locked_pair_count);
        }
    }

    return false;
}

You must log in to answer this question.

Browse other questions tagged .