0

I'm currently doing the Tideman PSET03 and I've completed almost everything except ``. Could you tell me why this is happening? It'd appreciated.

Check50: https://submit.cs50.io/check50/2d423edcaaba17336982db9d5ef647d084abd19e

lock_pairs function code:

bool check_pair(int target, int loser)
{
    // Loop through pairs
    for (int i = 0; i < pair_count; i++)
    {
        // If this pair is locked:
        if (locked[pairs[i].winner][pairs[i].loser] == true)
        {
            int pair_winner = pairs[i].winner;
            int pair_loser = pairs[i].loser;
            // Is there any pair with loser of the pair as a winner
            if (loser == pair_winner)
            {
                // If yes, Is the loser of this pair equal to the target return true
                if (pair_loser == target)
                {
                    return true;
                }
                // If not call the recursive function and pass the new loser
                else
                {
                    return check_pair(target, pair_loser);
                }
            }
        }
    }

    // If not makes a cycle return false
    return false;
}


// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    // TODO
    // Calling the recursive check_edge function
    bool cycle;
    for (int i = 0; i < pair_count; i++)
    {
        cycle = check_pair(pairs[i].winner, pairs[i].loser);

        if (!cycle)
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }
    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);
bool check_pair(int index_targer, int index_pair);
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 preferences array
    for (int i = 0; i < candidate_count - 1; i++)
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            // If they are the same candidate continue the inner for loop otherwise asign their winners
            if (preferences[i][j] == preferences[j][i])
            {
                continue;
            }
            else if (preferences[i][j] > preferences[j][i])
            {
                pairs[pair_count].winner = i;
                pairs[pair_count].loser = j;
                pair_count++;
            }
            else if (preferences[i][j] < preferences[j][i])
            {
                pairs[pair_count].winner = j;
                pairs[pair_count].loser = i;
                pair_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;
}

bool check_pair(int target, int loser)
{
    // Loop through pairs
    for (int i = 0; i < pair_count; i++)
    {
        // If this pair is locked:
        if (locked[pairs[i].winner][pairs[i].loser] == true)
        {
            int pair_winner = pairs[i].winner;
            int pair_loser = pairs[i].loser;
            // Is there any pair with loser of the pair as a winner
            if (loser == pair_winner)
            {
                // If yes, Is the loser of this pair equal to the target return true
                if (pair_loser == target)
                {
                    return true;
                }
                // If not call the recursive function and pass the new loser
                else
                {
                    return check_pair(target, pair_loser);
                }
            }
        }
    }

    // If not makes a cycle return false
    return false;
}


// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    // TODO
    // Calling the recursive check_edge function
    bool cycle;
    for (int i = 0; i < pair_count; i++)
    {
        cycle = check_pair(pairs[i].winner, pairs[i].loser);

        if (!cycle)
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }
    return;
}

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

    // Iterating through the locked array to get the possible winner of the election
    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;
            }
        }
    }

    // If there is a false in the no_winner array that means is the winner of the election
    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 .