0

I'm mostly concerned about the sort_pairs() function atm, but i'm willing to receive answers regarding the whole code. I can't really see what's wrong with the way i coded it in general, but mostly the sortpairs() function, that even though i've put some test printfs before and after the sorting of the pairs struct and they show that it was rearranged, check50 still gives me an error. I'm really stuck in this problem for almost a week and i know it's considered difficult but i'm still feeling down.

These are the results that came up when i ran it with 4 candidates (a, b, c, d) and 5 voters:

113111 // votes difference between winner and loser attributed to prefdif int array
01 20 30 12 31 32 // winner and loser from all the pairs before the sort
30 12 31 32 01 20 // winner and loser from all the pairs after the sort

The errors:

:( sort_pairs sorts pairs of candidates by margin of victory
    sort_pairs did not correctly sort pairs
:( lock_pairs locks all pairs when no cycles
    lock_pairs did not lock all pairs
:( lock_pairs skips final pair if it creates cycle
    lock_pairs did not correctly lock all non-cyclical pairs
:( lock_pairs skips middle pair if it creates a cycle
    lock_pairs did not correctly lock all non-cyclical pairs
:( print_winner prints winner of election when one candidate wins over all others
    print_winner did not print winner of election
:( print_winner prints winner of election when some pairs are tied
    print_winner did not print winner of election

The code in itself:

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    int prefdif[pair_count];
    int c = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] > 0)
            {
                if (i < j && preferences[i][j] > preferences[j][i])
                {
                    prefdif[c] = preferences[i][j] - preferences[j][i];
                    c++;
                }
            else if (i < j && preferences[i][j] < preferences[j][i])
                {
                    prefdif[c] = preferences[j][i] - preferences[i][j];
                    c++;
                }
            }
        }
    }

    // test prefdif result
    for (int i = 0; i < pair_count; i++)
    {
        printf("%i", prefdif[i]);
    }
    printf("\n");

    // test pairs before
    for (int i = 0; i < pair_count; i++)
    {
        printf("%i%i ", pairs[i].winner, pairs[i].loser);
    }
    printf("\n");



    int pair_strength[pair_count];
    int pair_str_pos[pair_count];


    for (int v = 0; v < pair_count; v++)
    {
        for (int i = 0; i < pair_count; i++)
        {
            if (i == 0)
            {
                pair_str_pos[v] = i;
            }
            else if (prefdif[i] > prefdif[i-1])
            {
                pair_str_pos[v] = i;
                pair_strength[v] = prefdif[i];
            }
            if (i == pair_count - 1)
            {
                prefdif[pair_str_pos[v]] = 0;
            }
        }
    }

    pair parallel[MAX * (MAX - 1) / 2];

    for (int i = 0; i < pair_count; i++)
    {
        parallel[i].winner = pairs[pair_str_pos[i]].winner;
        parallel[i].loser = pairs[pair_str_pos[i]].loser;
    }

    for (int i = 0; i < pair_count; i++)
    {
        pairs[i].winner = parallel[i].winner;
        pairs[i].loser = parallel[i].loser;
    }

    // test pairs after

    for (int i = 0; i < pair_count; i++)
    {
        printf("%i%i ", pairs[i].winner, pairs[i].loser);
    }

    printf("\n");


    return;
}

void checklock(int m, int n, int a, int b)
    {
        if (locked[m][n])
        {
            if (n == a)
            {
                locked[m][n] = false;
            }
            checklock(m, n + 1, a, b);
        }
        else
        {
            if (n == pair_count - 1)
            {
                if (m == pair_count - 1)
                {
                    return;
                }
                checklock(m + 1, 0, a, b);
            }
            else
            {
                checklock(m, n + 1, a, b);
            }
        }
    }

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{

    int chains[72];
    int chain_count = 0;





    for (int i = 0; i < (pair_count * 2); i += 2)
    {
        chains[i] = pairs[i].winner;
        chains[i+1] = pairs[i].loser;
        locked[chains[i]][chains[i+1]] = true;
    }



    for (int i = 0; i < pair_count; i++)
    {
        for (int j = 0; j < pair_count; j++)
        {
            if (locked[i][j])
            {
                checklock(i, j, i, j);
            }
        }
        return;
    }






// test checklock
/*
for (int i = 0; i < pair_count; i++)
    {
        for (int j = 0; j < pair_count; j++)
        {
            if (locked[i][j])
            {
                printf("%i%i", i,j);
            }
        }
        return;
    }
*/

    return;
}


// Print the winner of the election
void print_winner(void)
{

    if (locked[pairs[0].winner][pairs[0].loser])
    {
        printf("%s", candidates[pairs[0].winner]);
    }





    /* for (int i = 0; i < MAX; i++)
    {
        w = pairs[i].winner;
        l = pairs[i].loser;

        if (locked[w][l])
        {
            if (l != the_winner)
            {
                the_winner = w;
            }
        }

    } */
    return;
}




You must log in to answer this question.

Browse other questions tagged .