I have no idea why my code could not compile. Need some help/advise! Attached below 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]; //no. of pairs= n(n-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 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(candidates[i],name)==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 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= j;
pairs[pair_count].loser= i;
pair_count++;
}
else 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)
{
// TODO
for(int i = pair_count-1; i >=0; i--) // This nested loop is for bubble sort. Need i loop so that ith pair can start comparing. i.e after the 1st pair is done comparing with the rest of the pairs, the 2nd pair starts comparing with all other pairs so on&so forth)
{ // Need start i from decreasing count so that each new pair will compare with lesser number of pairs as the pairs at the back has already been compared previously.
for(int j=0; j <= i-1; j++) // j loop is to compare ith pair with all other j pair.
{
if((preferences[pairs[j].winner][pairs[j].loser]-preferences[pairs[j].loser][pairs[j].winner]) < (preferences[pairs[j+1].winner][pairs[j+1].loser]-preferences[pairs[j+1].loser][pairs[j+1].winner]))
{
pair temp= pairs[j];
pairs[j]= pairs[j+1];
pairs[j+1]= temp;
}
}
}
return;
}
bool hascycle (int winner, int loser)
{
for(int i=0; i < candidate_count; i++)
{
bool found= false;
while(winner!=loser && winner!= -1)
{
if(locked[i][winner])
{
winner= i;
found= true;
}
if(!found)
winner= -1;
}
if(winner==loser)
{
return true;
}
return false;
}
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
// TODO
for(int i=0; i < pair_count; i++)
{
if(!hascycle(pairs[i].winner, pairs[i].loser))
locked(pairs[i].winner, pairs[i].loser)= true;
}
return;
}
// Print the winner of the election
void print_winner(void)
{
// TODO
for(int i=0; i< candidate_count; i++)
{
int false_count=0;
for(j=0; j<candidate_count; j++)
{
if(locked[j][i]==false)
{
false_count++;
if(false_count==candidate_count)
{
printf("%s\n", candidates[i]);
}
}
}
}
return;
}
Warnings:
:) 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