I'm trying to solve assignment "Plurality" of Problem set 3. However, check50 didn't prove to be quite useful for me this time. I'm having only one error message saying "print_winner" function didn't identify "Bob" as the winner of the election. But when I did some manual check, my programe worked fine. Can someone help me find the bug(s) in my code?
My code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedef struct
{
string name;
int votes;
}
candidate;
// Array of candidates
candidate candidates[MAX];
// Number of candidates
int candidate_count;
// Function prototypes
bool vote(string name);
void print_winner(void);
int main(int argc, string argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [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].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count = get_int("Number of voters: ");
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
bool is_found = false;
int candidate_index;
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
is_found = true;
candidate_index = i;
break;
}
else
{
is_found = false;
}
}
if (is_found == true)
{
candidates[candidate_index].votes += 1;
return true;
}
else
{
return false;
}
}
// Print the winner (or winners) of the election
void print_winner(void)
{
//Sorting the array of candidates to descending order
for (int i = 0; i < candidate_count; i++)
{
int current = candidates[i].votes;
for (int j = i + 1; j < candidate_count; j++)
{
if (candidates[j].votes > current)
{
candidate temp = candidates[i];
candidates[i] = candidates[j];
candidates[j] = temp;
}
}
}
//Searching for ties in votes
int number_of_ties = 0;
for (int i = 0; i < candidate_count; i++)
{
int current = candidates[i].votes;
for (int j = i + 1; j < candidate_count; j++)
{
if (candidates[j].votes == current)
{
number_of_ties += 1;
}
}
}
//Checking if the tie is between the first and the second element of the array
if (number_of_ties != 0 && candidates[0].votes != candidates[1].votes)
{
number_of_ties = 0;
}
//Printing out the winner in case of no ties
if (number_of_ties == 0)
{
printf("%s\n", candidates[0].name);
}
//Printing out the winners in case of one tie
else if (number_of_ties == 1)
{
for (int i = 0; i <= number_of_ties; i++)
{
printf("%s\n", candidates[i].name);
}
}
//Printing out the winners in case of more than one tie
else
{
for (int i = 0; i < number_of_ties; i++)
{
printf("%s\n", candidates[i].name);
}
}
return;
}
My results on check50:
- :) plurality.c exists
- :) plurality compiles
- :) vote returns true when given name of first candidate
- :) vote returns true when given name of middle candidate
- :) vote returns true when given name of last candidate
- :) vote returns false when given name of invalid candidate
- :) vote produces correct counts when all votes are zero
- :) vote produces correct counts after some have already voted
- :) vote leaves vote counts unchanged when voting for invalid candidate
- :) print_winner identifies Alice as winner of election
- :( print_winner identifies Bob as winner of election Cause print_winner function did not print winner of election
- :) print_winner identifies Charlie as winner of election
- :) print_winner prints multiple winners in case of tie
- :) print_winner prints all names when all candidates are tied