0

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;
}
  • hey, have you solved it? i'm having the same issue here – domster May 19 at 17:40
  • In your add_pairs function, shouldn't winsize by updated incrementally, such that pairs[pair_count].winsize++; instead of pairs[pair_count].winsize = preferences[i][j] ? Not sure if this will help. – RandomForestRanger Jun 8 at 8:38
1

check50 doesn't care about how you implemented add_pairs when it's testing sort_pairs, so it doesn't initialize the winsize property. You'll need to remove that property, and access it in sort_pairs. In the problem directions it says

You should not modify anything else in tideman.c other than the implementations of the vote, record_preferences, add_pairs, sort_pairs, lock_pairs, and print_winner functions (and the inclusion of additional header files, if you’d like). You are permitted to add additional functions to tideman.c, so long as you do not change the declarations of any of the existing functions.

You also modified the typedef of pair, which you can't do with their automated testing.

Basically, your problem probably works as a whole, but the individual functions don't work the way the CS50x people want them to work, so the automated function-by-function testing doesn't work.

| improve this answer | |

You must log in to answer this question.

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