0

I have such a strange behavior here in pset 3, tideman. I am doing the print_winner function because check50 says everything else is fine, but when i run the program it looks like print_winner does not exist, there is no error in compiling but even the first printf that I put is missing. I have declared de function so I am completely lost here. Here is 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];

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 winner, int loser);
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(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 + 1; j < candidate_count; j++)
        {
            preferences[ranks[i]][ranks[j]] += 1;
        }
    }
// TODO
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++;
            }
        }
    }
// TODO
   return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    int strength[pair_count];

    for (int i = 0; i < pair_count; i++)
    {
        strength[i] = preferences[pairs[i].winner][pairs[i].loser] - preferences[pairs[i].loser] 
            [pairs[i].winner];
    }

    for(int i = 0; i < pair_count - 1; i++)
    {
        for(int j = i + 1; j < pair_count; j++)
        {
            if (strength[i] < strength[j])
            {
                pair tmp = pairs[i];
                pairs[i] = pairs[j];
                pairs[j] = tmp;
            }
        }
    }
// TODO
    return;
}

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

bool check_cycle(int winner, int loser)
{
    if (loser == winner)
    {
        return true;
    }

    for (int i = 0; i < candidate_count; i++)
    {
        if (locked[loser][i] == true && check_cycle(winner, i) == true)
        {
        // if the chain comes to the original winner, we have created a cycle
            return true;
    }
}
//in this point there is no cycle so return true to lock the pair
return false;
} 

// Print the winner of the election
void print_winner(void)
{

    printf("reached this point"); //I put this here to see if the program shows it but it doesn't

    int strength[pair_count];
    int champions[pair_count];

    for (int i = 0; i < pair_count; i++)
    {
        strength[i] = preferences[pairs[i].winner][pairs[i].loser] - preferences[pairs[i].loser] 
            [pairs[i].winner];
    
    }

    champions[0] = pairs[0].winner;

    for (int i = 1; i < pair_count; i++)
    {
         if (strength[i] == strength[i - 1])
         {
              champions[i] = pairs[i].winner;
         }
         else
         {
             break;
         }
     }

    printf("The winners of the election are:");
    
    for (int i = 0; i < strlen(champions); i++)
    {
        printf("%s \n", candidates[i]);   
    }
    return;
}

Thank you so much for any help, I really appreciate it.

1

Have you compiled the program locally? The compiler should complain about this strlen(champions) because champions is declared an int array here int champions[pair_count]

1
  • You are right, I didn't clang it and now it complains about that. I'm still trying to make print_winner work but thank you so much! – Alejandro Sánchez Jan 23 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .