I have some code that completely passes check50, but prints results way too many times. For instance, here's a typical output:
~/pset3/plurality/ $ ./plurality Alice Bob Carol
Number of voters: 5
Vote: Alice
Vote: Carol
Vote: Bob
Vote: Carol
Vote: Alice
Carol
Alice
Carol
Alice
Carol
Alice
Carol
Alice
Carol
Alice
Two other examples:
~/pset3/plurality/ $ ./plurality Alice Bob Carol
Number of voters: 2
Vote: Bob
Vote: Bob
Bob
Bob
Bob
~/pset3/plurality/ $ ./plurality Alice Bob Carol
Number of voters: 1
Vote: Alice
Alice
Alice
Alice
Alice
Three results for 2, four results for 1... I don't get it at all.
Here's the code for the two functions I was supposed to change:
// 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)
// 0 means both are the same in strcmp
{
candidates[i].votes++;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// i leads to (candidate_count - 1) so I don't get a buffer overrun
// accounts for the algorithim counting i+1 at the same time
for (int i = 0; i < (candidate_count); i++)
{
if (candidates[i].votes > candidates[(i + 1)].votes)
{
// swaps the two variable positions
int tempv = candidates[i].votes;
string tempn = candidates[i].name;
candidates[i].votes = candidates[(i + 1)].votes;
candidates[i].name = candidates[(i + 1)].name;
candidates[(i+1)].votes = tempv;
candidates[(i+1)].name = tempn;
// my intention is to restart the whole process every time I move a variable back
// i think this recursion will make it loop back enough times to get everything
print_winner();
}
}
// print last candidate on list, line break, and then repeat for any other with same amount of votes
for (int i = 0; i < (candidate_count); i++)
{
if (candidates[candidate_count].votes == candidates[(candidate_count - i)].votes)
{
printf("%s\n", candidates[(candidate_count - i)].name);
}
else
break;
}
}
I've tried to debug this and didn't understand what was happening. I'll put the code for the rest of the program below, although I believe I haven't changed this all. Thank you very much for your time.
#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();
}