gorfungus
gorfungus

Reputation: 1

CS50 Tideman Logic Flaw?

This is my current implementation of tideman. I am unsure as to there are no pairs locked, when I debug it. Please help me it has been 5 days.

#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);
bool check_cycle(int loser, int winner, int middleman);
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];
    }

    // 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[])
{
    int voted = 11;
    for (int z = 0; z < candidate_count; z++)
    {
        if (strcmp(candidates[z], name) == 0)
        {
            voted = z;
            break;
        }
    }

    if (voted == 11)
    {
        return false;
    }

    ranks[rank] = voted;
    return true;
}

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

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] == preferences[j][i])
            {
                continue;
            }

            if (i == j)
            {
                continue;
            }

            if (preferences[i][j] > preferences[j][i])
            {
                pairs[pair_count].winner = i;
                pairs[pair_count].loser = j;
                pair_count++;
            }
        }
    }
    return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        for (int j = 0; j < pair_count; j++)
        {
            if (preferences[pairs[i].winner][pairs[i].loser] > preferences[pairs[j].winner][pairs[j].loser])
            {
                if (i > j)
                {
                    pair temp;
                    temp = pairs[j];
                    pairs[j] = pairs[i];
                    pairs[i] = temp;
                }
            }
        }
    }
    return;
}

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    for (int i = 0; i < pair_count - 1; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            int loser = pairs[i].loser;
            int winner = pairs[i].winner;
            int cycles = 0;

            if (cycles > 0)
            break;

            if (locked[loser][j] == false)
            {
                continue;
            }

            else if (check_cycle(loser, winner, j) == false)
            {
                cycles++;
                continue;
            }

            if (j == candidate_count - 1 && cycles == 0)
            {
                locked[winner][loser] = true;
            }
        }
    }
    return;
}

//check_cycle has some logic issue, resolve tmr
bool check_cycle(int loser, int winner, int middleman)
{
    if (locked[middleman][winner] == true)
    {
        return false;
    }

    for (int k = 0; k < candidate_count; k++)
    {

        if (locked[middleman][k] == false)
        continue;

        else
        {
            if (check_cycle(loser, winner, k) == false)
            {
                return false;
            }
        }
    }
    return true;
}


// Print the winner of the election
void print_winner(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        int check = 0;

        for (int k = 0; k < candidate_count; k++)
        {
            if (locked[k][i] == 1)
            check++;
        }

        if (check == 0)
        {
            printf("%s\n", candidates[i]);
            break;
        }
    }
    return;
}

I expected for my j loop in locked pairs to check for whether loser had a locked pair with each candidate, skipping the iteration if no locked pair was found. If a locked pair was found, check_cycle would check for whether the current candidate j, i.e. middleman, had a locked pair over the winner, returning false if so. check_cycles would also check if the middleman had any other locked pairs in the graph. If check_cycles found that the middleman was locked over candidate k in the graph, the new check_cycles with k would repeat. if that returned false, or if any further chain in the iteration returned false, the original check_cycle would return false as well, resulting in int cycles++, which would lead to the current pair not being locked. However, in debugging, the locked matrix just has all false.

Upvotes: 0

Views: 56

Answers (0)

Related Questions