Reputation: 25
I checked my code using check50, all the criteria are satisfied other than one, the print_winner()
function isn't printing multiple winners in case of a tie, I don't know why it isn't working.
these are my results:
:) 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
:) print_winner identifies Charlie as winner of election
:( print_winner prints multiple winners in case of tie
print_winner function did not print both winners of election
:) print_winner prints all names when all candidates are tied
What should I fix in the following 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;
int voter_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;
}
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) {
for (int i = 0; i < candidate_count; i++) {
if (strcmp(candidates[i].name, name) == 0) {
candidates[i].votes++;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
string x;
void print_winner(void) {
for (int i = 0; i < candidate_count - 1; i++) {
for (int j = i + 1; j < candidate_count; j++) {
if (candidates[j].votes < candidates[i].votes) {
// swapping elements
x = candidates[i].name;
candidates[i].name = candidates[j].name;
candidates[j].name = x;
}
}
}
for (int k = 0; k < candidate_count; k++) {
if (candidates[k].votes == candidates[candidate_count - 1].votes) {
printf("%s\n", candidates[k].name);
}
}
return;
}
Upvotes: 0
Views: 593
Reputation: 144969
Your sorting code only sorts the candidate names, not the candidate votes. Note that there is no need to sort the candidates by vote count, you can just determine the maximum number of votes and print all candidates with this number of votes. The function should be renamed print_winners()
for consistency. Also remove the global variable x
, which should have been local to print_winner()
anyway.
Here is a modified version:
void print_winners(void) {
int max_votes = candidates[0].votes;
for (int i = 1; i < candidate_count; i++) {
if (max_votes < candidates[i].votes) {
max_votes = candidates[i].votes;
}
}
for (int i = 0; i < candidate_count; i++) {
if (candidates[i].votes == max_votes) {
printf("%s\n", candidates[i].name);
}
}
}
Upvotes: 1