0

I'm currently doing the tideman pset and i have some errors on the add_pairs function and the lock_pais function but I'm more focused now on the add_pairs function. I don't know why these errors are happening. I'd like to know what they mean and why are happening. Please, help me on these problems: :( add_pairs generates correct pair count when ties exist and :( add_pairs fills pairs array with winning pairs. It'd be appreciated.

Check50: https://submit.cs50.io/check50/3c67f3e61928d7a9707781c712ec69c659663a64

add_pairs function:

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO
    // Iterating through the pairs array
    bool alreadywon;
    bool theres_winner;
    bool won;
    int index;
    int p;
    do
    {
        index = pair_count;
        won = false;
        for (p = 0; p < candidate_count; p++)
        {
            // If the preferences is not 0
            // If we have a winner:
            if (pair_count != 0 && pairs[pair_count - 1].winner == 0 && pairs[pair_count - 1].loser == 0)
            {
                index = pair_count - 1;
            }

            theres_winner = (preferences[pair_count][p] > preferences[p][pair_count]);
            alreadywon = ((pair_count == candidate_count - 1 || p != 0) && pairs[pair_count - 1].winner == pair_count && pairs[pair_count - 1].loser == p);

            if (preferences[pair_count][p] != 0)
            {
                if ((preferences[pair_count][p] == preferences[p][pair_count]) || !theres_winner || alreadywon)
                {
                    continue;
                }

                if (theres_winner)
                {
                    // Assigning:
                    pairs[index].winner = pair_count;
                    pairs[index].loser = p;
                    pair_count++;
                    won = true;
                    break;
                }
            }
        }

        if (won != true)
        {
            pair_count++;
        }

        if (won == true && pair_count == candidate_count && preferences[pair_count - 1][p + 1] > preferences[p + 1][pair_count - 1])
        {
            pair_count--;
        }
    }
    while (pair_count != candidate_count);
    return;
}

Tideman Code:

#include <cs50.h>
#include <stdio.h>
#include <strings.h>

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: tideman [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i] = argv[i + 1];
    }

    for (int f = 0; f < MAX; f++)
    {
        for (int k = 0; k < MAX; k++)
        {
            preferences[f][k] = 0;
        }
    }

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
    // TODO
    // Iterating to check if the name is a valid cantidate, if so update the ranks array
    for (int j = 0; j < candidate_count; j++)
    {
        //Comparing the name with a valid candidate and returning true
        if (strcasecmp(name, candidates[j]) == 0)
        {
            // Checking the candidate has been voted
            for (int f = rank - 1; f >= 0; f--)
            {
                if (ranks[f] == j)
                {
                    return false;
                }
            }
            ranks[rank] = j;
            return true;
        }
    }
    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // TODO
    // Iterating trough each voters' ranks
    for (int e = 0; e < candidate_count; e++)
    {
        for (int d = 0; d < candidate_count; d++)
        {
            // If comparing the same candidate, asign 0
            if (ranks[e] == ranks[d])
            {
                preferences[ranks[e]][ranks[d]] = 0;
            }
            // If the candidate has more votes, add 1
            if (e < d)
            {
                preferences[ranks[e]][ranks[d]] = preferences[ranks[e]][ranks[d]] + 1;
            }
        }
    }
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO
    // Iterating through the pairs array
    bool alreadywon;
    bool theres_winner;
    bool won;
    int index;
    int p;
    do
    {
        index = pair_count;
        won = false;
        for (p = 0; p < candidate_count; p++)
        {
            // If the preferences is not 0
            // If we have a winner:
            if (pair_count != 0 && pairs[pair_count - 1].winner == 0 && pairs[pair_count - 1].loser == 0)
            {
                index = pair_count - 1;
            }

            theres_winner = (preferences[pair_count][p] > preferences[p][pair_count]);
            alreadywon = ((pair_count == candidate_count - 1 || p != 0) && pairs[pair_count - 1].winner == pair_count && pairs[pair_count - 1].loser == p);

            if (preferences[pair_count][p] != 0)
            {
                if ((preferences[pair_count][p] == preferences[p][pair_count]) || !theres_winner || alreadywon)
                {
                    continue;
                }

                if (theres_winner)
                {
                    // Assigning:
                    pairs[index].winner = pair_count;
                    pairs[index].loser = p;
                    pair_count++;
                    won = true;
                    break;
                }
            }
        }

        if (won != true)
        {
            pair_count++;
        }

        if (won == true && pair_count == candidate_count && preferences[pair_count - 1][p + 1] > preferences[p + 1][pair_count - 1])
        {
            pair_count--;
        }
    }
    while (pair_count != candidate_count);
    return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO
    int winner;
    int loser;
    int winner_next;
    int loser_next;
    pair tmp;
    // Iterating through the pairs array
    for (int h = 0; h < pair_count - 1; h++)
    {
        // Getting the winner and the loser of the 2 next pairs' values, if the first one has less strength of victory swap them (using bubble sort)
        winner = pairs[h].winner;
        loser = pairs[h].loser;
        winner_next = pairs[h + 1].winner;
        loser_next = pairs[h + 1].loser;
        if (preferences[winner][loser] < preferences[winner_next][loser_next])
        {
            tmp = pairs[h];
            pairs[h] = pairs[h + 1];
            pairs[h + 1] = tmp;
        }
    }
    return;
}

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    // TODO
    int winner;
    int loser;
    bool edge = false;
    // Iterating trough the locked pairs
    for (int v = 0; v < pair_count; v++)
    {
        // Getting the winner and loser
        winner = pairs[v].winner;
        loser = pairs[v].loser;
        // Checking it it has a edge or not
        if (v != pair_count - 1)
        {
            locked[winner][loser] = true;
        }
        else
        {
            for (int f = 0; f < pair_count; f++)
            {
                if (locked[loser][f] == true)
                {
                    edge = true;
                }
            }

            if (edge != true)
            {
                locked[winner][loser] = true;
            }
        }
    }
    return;
}

// Print the winner of the election
void print_winner(void)
{
    // TODO
    string winner;
    bool no_winner[pair_count];
    for (int t = 0; t < pair_count; t++)
    {
        no_winner[t] = false;
    }

    for (int l = 0; l < pair_count; l++)
    {
        for (int p = 0; p < pair_count; p++)
        {
            if (locked[p][l] == true)
            {
                no_winner[l] = true;
            }
        }
    }

    for (int w = 0; w < pair_count; w++)
    {
        if (no_winner[w] == false)
        {
            winner = candidates[w];
            printf("%s\n", winner);
            return;
        }
    }
}

0

You must log in to answer this question.

Browse other questions tagged .