0

my program has apparently one bug. check50 says my program does NOT skip the last pair if it creates cycle.

However, I ran my function through debug and it did skip the last pair and did not write anything into locked array.

Could you, please, point out what causes the bug?

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    // iterate through each pair in pairs
    for (int i = 0; i < pair_count; i++)
    {
        bool has_cycle = false;
        // get index of winner and loser
        int winner = pairs[i].winner;
        int loser = pairs[i].loser;


        // check for cycles
        for (int row = loser, loop = 1; loop;)
        {
            for (int col = 0; col < candidate_count; col++)
            {
                // is there a lock?
                if (locked[row][col])
                {
                    // cycle not found, keep digging
                    if (col != winner)
                    {
                         row = col;
                         col = -1;
                         continue;
                    }

                    // cycle found
                    has_cycle = true;
                    break;
                }
            }
            // code gets to this stage if it did not found any lock on col positions
            // therefore a cycle has not been created and we can exit the loop
            loop = 0;
        }

        // lock pair if it does not create cycle
        if(!has_cycle)
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }
    return;
}

You must log in to answer this question.

Browse other questions tagged .