The program should count which candidate get the most votes; and should act like:
$ ./plurality Alice Bob
Number of voters: 3
Vote: Alice
Vote: Charlie
Invalid vote.
Vote: Alice
Alice
If there are multiple candidates with a same number of votes program should print out 2 names:
$ ./plurality Alice Bob Charlie
Number of voters: 5
Vote: Alice
Vote: Charlie
Vote: Bob
Vote: Bob
Vote: Alice
Alice
Bob
For more information check out https://cs50.harvard.edu/x/2023/psets/3/plurality/
Here is my version of the 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;)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
else
{
i++;
}
}
// 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(name, candidates[i].name) == 0)
{
candidates[i].votes = candidates[i].votes + 1;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
char t[100];
for (int i = 0; i < candidate_count; i++)
{
for (int j = 0; j < candidate_count - i - 1; j++)
{
if (candidates[j].votes < candidates[j + 1].votes)
{
int temp = candidates[j].votes;
candidates[j].votes = candidates[j + 1].votes;
candidates[j + 1].votes = temp;
strcpy(t, candidates[j].name);
strcpy(candidates[j].name, candidates[j + 1].name);
strcpy(candidates[j + 1].name, t);
}
}
}
printf("%s\n", candidates[0].name);
for(int k = 1; k < candidate_count; k++)
{
if (candidates[0].votes == candidates[k].votes)
{
printf("%s\n", candidates[k].name);
}
}
return;
}
It works with examples i come up with, however automatic program check50 which is used to check answer 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 function did not print winner of election
:( print_winner identifies Charlie as winner of election
print_winner function did not print winner of election
:) print_winner prints multiple winners in case of tie
:) print_winner prints all names when all candidates are tied
I can't see what the problem is, and my debugger doesn't show candidate[j].votes or candidate[j].name values, so i'm lost. Please help.
Edit: array t contains 100 elements so i wont get a segmentation fault, eventho the task doesn't require strings of bigger sizes, so it isn't where problem is