CS50 Tideman print_winner

Viewed 118

I've completed my print_winner function. It prints the winner but when I run check50 it shows me this error

:( print_winner prints winner of election when some pairs are tied

print_winner did not print winner of election

I am confused because in 'Specification' it was told "You may assume there will not be more than one source.". If I understood this correct, it means that there is only one winner.

Problem description provided here

Here is my full code

#include <cs50.h>
#include <stdio.h>
#include <string.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 sequence(int loser, int first_winner);

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];
    }

    // 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[])
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(name, candidates[i]) == 0)
        {
            ranks[rank] = i;
            return true;
        }
    }
    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            preferences[ranks[i]][ranks[j]]++;
        }
    }
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO
    for (int j = 0; j < candidate_count; j++)
    {
        for (int k = j + 1; k < candidate_count; k++)
        {
            if (preferences[j][k] > preferences[k][j])
            {
                pairs[pair_count].winner = j;
                pairs[pair_count].loser = k;
                pair_count++;
            }
            else if (preferences[k][j] > preferences[j][k])
            {
                pairs[pair_count].winner = k;
                pairs[pair_count].loser = j;
                pair_count++;
            }
        }
    }
return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO
    for (int j = 0; j < pair_count - 1; j++)
    {
        int shifts = 0;
        for (int i = 0; i < pair_count - 1 - j; i++)
        {
            if (preferences[pairs[i].winner][pairs[i].loser] < preferences[pairs[i + 1].winner][pairs[i + 1].loser])
            {
                pair variable;
                variable.winner = pairs[i].winner;
                variable.loser = pairs[i].loser;

                pairs[i].winner = pairs[i + 1].winner;
                pairs[i].loser = pairs[i + 1].loser;

                pairs[i + 1].winner = variable.winner;
                pairs[i + 1].loser = variable.loser;

                shifts++;
            }
        }
        if (shifts == 0)
        {
            break;
        }
    }
    return;
}

int sequence(int loser, int first_winner)
{
    // Count of locks to first winner
    int lock = 0;

    // Count of wins of the loser
    int count = 0;

    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[loser][i] == true)
        {
            count++;
        }
    }

    // Base case 1, if loser won first winner
    if (loser == first_winner)
    {
        lock++;
        return lock;
    }

    // Base case 2, if loser didn't win anyblode
    if (count == 0)
    {
        return lock;
    }

    for (int i = 0; i < candidate_count; i++)
    {
        // If loser won anybody
        if (locked[loser][i] == true)
        {
            // If in this recursive sequence first winner was defeated
            if (sequence(i, first_winner) != 0)
            {
                return 1;
            }
        }
    }
    // If first winner was not defeated
    return 0;
}


// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    //
    for (int i = 0; i < pair_count; i++)
    {
            if (sequence(pairs[i].loser, pairs[i].winner) == 0)
            {
                locked[pairs[i].winner][pairs[i].loser] = true;
            }
    }
    return;
}

// Print the winner of the election
void print_winner(void)
{
    // Iterates through all the pairs
    for (int i = 0; i < pair_count; i++)
    {
        int lock = 0;
        // Iterates through all the candidates
        for (int j = 0; j < candidate_count; j++)
        {
            // If somebody won the winner of a pair - programm skips whim
            if (locked[j][pairs[i].winner] == true)
            lock++;
        }
        // If nobody won the winner of a pair - programm prints his hame and exits
        if (lock == 0)
        {
            printf("%s\n",candidates[pairs[i].winner]);
            return;
        }
    }
    return;
}

If I change my print_winner function like this. "Check50" accepts it. However, I can not understand what is the difference between the version of 'print_winner' in the snippet and the version in the full code

void print_winner(void)
{
    // Iterates through all the pairs
    for (int i = 0; i < candidate_count; i++)
    {
        int lock = 0;
        // Iterates through all the candidates
        for (int j = 0; j < candidate_count; j++)
        {
            // If somebody won the winner of a pair - programm skips whim
            if (locked[j][i] == true)
            lock++;
        }
        // If nobody won the winner of a pair - programm prints his hame and exits
        if (lock == 0)
        {
            printf("%s\n",candidates[i]);
            return;
        }
    }
    return;
}
0 Answers
Related