Jordan
Jordan

Reputation: 1

How do I get my CS50 Tideman code to compile?

I'm having issues compiling the code for tideman, please help? Here's my 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];
pair swap[0];

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);
bool lock_check(int ii, int jj);

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[])
{
// TODO
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[])
{
for (int i = 0; i < candidate_count; i++)
{
    for (int j = i + j; 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)
{
for (int i = 0; i < candidate_count; i++)
{
    for (int j = i + 1; j < candidate_count; j++)
    {
        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)
{
//compare people that prefer winner to loser
for (int i = 0; i < pair_count; i++)
{
    for (int j = 0; j < pair_count - i - 1; j++)
    {
        int i1 = pairs[j].winner;
        int j1 = pairs[j].loser;

        int strength_pair1 = preferences[i1][j1] - preferences[j1][i1];

        int i2 = pairs[j + 1].winner;
        int j2 = pairs[j + 1].loser;

        int strength_pair2 = preferences[i2][j2] - preferences[j2][i2];

        if (strength_pair1 < strength_pair2)
        {
            swap[0] = pairs[j];
            pairs[j] = pairs[j + 1];
            pairs[j + 1] = swap[0];
        }
    }
}
return;
}

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

    if (lock_check(ii, jj))
    {
        locked[ii][jj] = false;
    }
}
return;
}

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

    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[j][i] == false)
        {
            counter1++;

            if (counter1 ++ candidate_count)

                printf("%s\n", candidates[j]);
            }
        }
    }

return;

}

bool lock_check(int ii, int jj)
{
if (candidate_count == 1)

return false;

if (ii == jj)

return true;

for (int i = 0; i < candidate_count; i++)
{
    if (locked[jj][i] == true)
    {
        if (lock_check(ii, i))
    return true;
    }
}

    return false;
}

Results for cs50/problems/2023/x/tideman generated by check50 v3.3.7 :) tideman.c exists :( tideman compiles code failed to compile :| vote returns true when given name of candidate can't check until a frown turns upside down :| vote returns false when given name of invalid candidate can't check until a frown turns upside down :| vote correctly sets rank for first preference can't check until a frown turns upside down :| vote correctly sets rank for all preferences can't check until a frown turns upside down :| record_preferences correctly sets preferences for first voter can't check until a frown turns upside down :| record_preferences correctly sets preferences for all voters can't check until a frown turns upside down :| add_pairs generates correct pair count when no ties can't check until a frown turns upside down :| add_pairs generates correct pair count when ties exist can't check until a frown turns upside down :| add_pairs fills pairs array with winning pairs can't check until a frown turns upside down :| add_pairs does not fill pairs array with losing pairs can't check until a frown turns upside down :| sort_pairs sorts pairs of candidates by margin of victory can't check until a frown turns upside down :| lock_pairs locks all pairs when no cycles can't check until a frown turns upside down :| lock_pairs skips final pair if it creates cycle can't check until a frown turns upside down :| lock_pairs skips middle pair if it creates a cycle can't check until a frown turns upside down :| print_winner prints winner of election when one candidate wins over all others can't check until a frown turns upside down :| print_winner prints winner of election when some pairs are tied can't check until a frown turns upside down

Upvotes: 0

Views: 99

Answers (1)

DinoCoderSaurus
DinoCoderSaurus

Reputation: 6520

At least one problem is here pair swap[0];. From the spec:

You should not modify anything else in tideman.c other than the implementations of the vote, record_preferences, add_pairs, sort_pairs, lock_pairs, and print_winner functions (and the inclusion of additional header files, if you’d like)

Upvotes: 0

Related Questions