0

I am trying to solve the tideman problem for pset3. I have managed to get all parts of my code working apart from the the two parts which relate to checking whether cycles are created.

:( lock_pairs skips final pair if it creates cycle
    lock_pairs did not correctly lock all non-cyclical pairs
:( lock_pairs skips middle pair if it creates a cycle
    lock_pairs did not correctly lock all non-cyclical pairs

I use the following function to check for cycles I have checked it using debug50 and have stepped through the entire execution of the program line-by-line and even though it reaches return true that value isn't returned and instead the execution jumps to the last } of this function then tries to loop through the second for loop before returning false. This seems very strange to me, I may be missing something really obvious here but, I would appreciate any help as I can't quite see the issue which is causing my problem.

bool cycle_check(int input_loser, int cycle_winner, int current_pair_level, int n_pass)
{
    if (n_pass > 0)
    {
        for (int i = 0; i < current_pair_level; i++)
        {
            if ((pairs[i].winner == input_loser) && (pairs[i].loser == cycle_winner))
            {
                return true;
            }
        }
    }
    for (int i = 0; i < current_pair_level; i++)
    {
        if (pairs[i].winner == input_loser)
        {
            cycle_check(pairs[i].loser, cycle_winner, current_pair_level, n_pass += 1);
        }
    }
    return false;
}

You must log in to answer this question.

Browse other questions tagged .