# Tideman sort_pairs function

I tested this sort function separately and it sorts alright, but somehow doesn't pass the check50 test. The pairs.strength is the winning strength, recorded previously in add_pairs function. All my other functions passed check50 except this one and I can't figure out what's wrong. Would appreciate any suggestions. Thanks!

``````// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
for (int i = 0; i < candidate_count -1; i++) //for each first candidate
{
for (int j = i + 1; j < candidate_count; j++) //paired with each second candidate
{
if (preferences[i][j] > preferences[j][i]) //if first candidate is preferred over second candidate
{
pairs[pair_count].winner = i; //add winners and losers
pairs[pair_count].loser = j;
pairs[pair_count].strength = preferences[i][j]; //record strength of win
pair_count++; //count pairs
}
else if (preferences[i][j] < preferences[j][i]) // if second candidate is preferred over first candidate
{
pairs[pair_count].winner = j;
pairs[pair_count].loser = i;
pairs[pair_count].strength = preferences[j][i];
pair_count++;
}
}
}
return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
pair first_unsorted;
for (int i = 0; i < pair_count - 1; i++) //for each first pair
{
for (int j = i + 1; j < pair_count; j++) //paired with a second pair
{

if (pairs[j].strength > pairs[i].strength) //if second strength larger than first, swap
{
first_unsorted = pairs[i];
pairs[i] = pairs[j];
pairs[j] = first_unsorted;
}
}
}

return;
}
``````
• did you modify the structure (struct) of pairs? i see you've another identifier, strength, apart from winnier and loser Commented May 19, 2020 at 7:18
• @domster yes, i modified the struct to contain winner, loser and strength. Also I gave up and used qsort function instead. Commented May 20, 2020 at 3:16
• alright i see. Cheers! Commented May 20, 2020 at 4:09

## 1 Answer

I don't know for a fact because I don't know how the check50 testing works, but I'd bet it's because of that extra strength property. Your program works all together, but the individual functions don't work like the CS50 people envisioned them. To solve this, you'll probably have to remove the extra strength property, and calculate that in the sort_pairs function (or as an extra function that you call inside sort_pairs).

More detail about how the testing probably works: Each of the tests sets up what the variables would be if all the rest of the program works like it should, then calls whatever function it's directly testing, and checks that the output is right, and the global variables are right. Specifically, in your case, they're setting up pairs to have a bunch of pairs that have winner and loser properties, but they're not setting the strength property because that's not in their idea of what should happen in add_pairs. So the strength property is filled with whatever random value it started as, instead of what it would be set it in add_pairs in your program. Your program isn't passing because it's sorting based on that random value, instead of the actual strength of the win.

(You can see that the check50 is looking at each function individually by changing back all but one of the functions you wrote, and running check50. The tests for the one function should pass, and the rest should fail.)