0

Before going to Python I thought I'd go back to some previous homework that I hadn't been able to solve - pset3/tideman.

I've spent ages on this but I just can't seem to figure out the lock function. What I think I'm doing is looking for a cycle by seeing whether, for locked[i][j], if [j][x] is true, seeing whether [x][something else] is true, and tracking that back (using recursion) checking each time whether that second value is the same as [i] - but that doesn't seem to be working.

Can anyone help? Any hints or advice would be greatly appreciated.

This is my code

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.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 checkloop(int x, int y);
bool cycle(int x);
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];
    }

    // 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(candidates[i], name) == 0) //compare name with candidates
        {
            ranks[rank] = i; //if name is found, update this voter's ranks array with the candidate's value
            return true;
        }
    }
    return false; //if the name isn't found, return false
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    for (int i = 0; i < candidate_count; i++) //cycle through candidates in a voter's ranks
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            preferences[(ranks[i])][(ranks[j])] += 1; //update preferences array with voter's ranking preferences
        }
    }
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{

    for (int i = 0; i < candidate_count; i++) //cycle through preferences array
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] > preferences[j][i]) //check for which candidates of a pair have won
            {
                pairs[pair_count].winner = i;
                pairs[pair_count].loser = j;
                pair_count++; //update pair count
            }

        }
    }

}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
   for (int i = 0; i < pair_count; i++) //for each item in the pairs list
   {
       for (int j = 0; j < pair_count - i; j++)
       {
           if (preferences[pairs[j].winner][pairs[j].loser] < preferences[pairs[(j + 1)].winner][pairs[(j + 1)].loser])
           {
               pair t = pairs[j];
               pairs[j] = pairs[j + 1];
               pairs[j + 1] = t;
           }
       }
   }
}

// Checks for locked edges
bool checkloop(int x, int y)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[x][i] == true)
        {
            if (i == y)
            {
                return true;
            }
            checkloop(i, y);
        }
    }
    return false;
}

// Checks whether a candidate was already locked in as a winner
bool cycle(int x)
{
    int z = pairs[x].loser;
    int y = pairs[x].winner;
    if (checkloop(z, y) == true)
    {
        return true;
    }
    return false;
}

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

// Print the winner of the election
void print_winner(void)
{
    int winner[candidate_count];
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (locked[i][j] == true)
            {
                winner[i]++;
            }

        }
    }

    int maxwins = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (winner[i] > maxwins)
        {
            maxwins = winner[i];
        }
    }

    for (int i = 0; i < candidate_count; i++)
    {
        if (maxwins == winner[i])
        {
            printf("%s\n", candidates[i]);
        }
    }

}

When I run check50, I get this:

:) tideman.c exists
:) tideman compiles
:) vote returns true when given name of candidate
:) vote returns false when given name of invalid candidate
:) vote correctly sets rank for first preference
:) vote correctly sets rank for all preferences
:) record_preferences correctly sets preferences for first voter
:) record_preferences correctly sets preferences for all voters
:) add_pairs generates correct pair count when no ties
:) add_pairs generates correct pair count when ties exist
:) add_pairs fills pairs array with winning pairs
:) add_pairs does not fill pairs array with losing pairs
:) sort_pairs sorts pairs of candidates by margin of victory
:) lock_pairs locks all pairs when no cycles
:( 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 prints winner of election when some pairs are tied
    print_winner did not print winner of election
To see the results in your browser go to https://submit.cs50.io/check50/ee381246161162d05a7bf9792c34ab13184683b5

You must log in to answer this question.

Browse other questions tagged .