1

Here is my code for the add_pairs() function. I have tested it with the debugger and it seems to work fine. However, check50 tells me that it's not generating correct pair count.

'''
void add_pairs(void)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (i != j)
            {
                if (preferences[i][j] > preferences[j][i])
                {
                    pairs[pair_count].winner = i;
                    pairs[pair_count].loser = j;
                    pair_count += 1;
                }
                else if (preferences[i][j] < preferences[j][i])
                {
                    pairs[pair_count].winner = j;
                    pairs[pair_count].loser = i;
                    pair_count += 1;
                }
                else
                {
                    return;
                }
            }
        }
    }
}
'''

I can't figure out where I am going wrong. Please can someone help?

Thanks in advance.

1 Answer 1

0

I don't know if I'm late you're going good here is my code if it can gives you some help k in my code means pair_count in yours

for (int k = 0; k < candidate_count; k++)
{
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (i != j)
            {
                if (preferences[i][j] > preferences[j][i])
                {
                        pairs[k].winner = i;
                        pairs[k].loser = j;
                        pair_count++;
                }
                else if (preferences[i][j] < preferences[j][i])
                {
                        pairs[k].winner = j;
                        pairs[k].loser = i;
                        pair_count++;

                }
            }
            if (preferences[i][j] > 0)
            {
                if (preferences[i][j] > preferences[j][i])
                {
                        pairs[k].winner = i;
                        pairs[k].loser = j;
                        pair_count++;
                }
                else if (preferences[i][j] < preferences[j][i])
                {
                        pairs[k].winner = j;
                        pairs[k].loser = i;
                        pair_count++;

                }
            }
        }
    }
}
return;

You must log in to answer this question.

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