I am trying to solve sort_pairs by including a property - int winsize - into the struct of pair.
The new pair struct is:
typedef struct
{
int winner;
int loser;
int winsize;
}
pair;
I include winsize when initializing pairs under add_pairs:
void add_pairs(void)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
for (int j = 0; j < candidate_count; j++)
{
if (i != j && preferences[i][j] > preferences[j][i])
{
pairs[pair_count].winner = i;
pairs[pair_count].loser = j;
pairs[pair_count].winsize = preferences[i][j];
pair_count += 1;
}
}
}
return;
}
When it comes to sort_pairs, all my test cases seem to sort in descending order, but check50 still highlights it as incorrect. I've tried the various edge cases I know, and incorporated answers from similar questions, but to no avail. I've tried both selection and bubble sort. Appreciate if someone could help! Thanks in advance.
void sort_pairs(void)
{
// TODO
int counter = -1;
pair temp;
while (counter != 0)
{
counter = 0;
for (int i = 0; i < pair_count-1; i++)
{
if (pairs[i].winsize < pairs[i+1].winsize)
{
temp = pairs[i];
pairs[i] = pairs[i+1];
pairs[i+1] = temp;
counter += 1;
}
}
}
return;
}
winsize
by updated incrementally, such thatpairs[pair_count].winsize++;
instead ofpairs[pair_count].winsize = preferences[i][j]
? Not sure if this will help.