Skip to content

Instantly share code, notes, and snippets.

@deeunix
Last active June 4, 2024 01:20
Show Gist options
  • Save deeunix/d9523f86b7499ba880055f836b48ba6d to your computer and use it in GitHub Desktop.
Save deeunix/d9523f86b7499ba880055f836b48ba6d to your computer and use it in GitHub Desktop.
my Cs50 plurality solution
#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)
{
//Loop through the Candidate count
for (int i = 0; i < candidate_count; i++)
{
//check if candidate is similar to what is user's vote
if (strcmp(candidates[i].name, name) == 0)
{
//if similar to what user vote for, then increase the specific candidate vote and return true
candidates[i].votes++;
return true;
}
}
//else if not similar to candidate vote return false.....and print invalid vote
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
//Create and variable and set it to 0
int maximum_vote = 0;
//iterate over list of candidate
for (int i = 0; i < candidate_count; i++)
{
//check for candidate votes that are greater than maximum_vote and set them to maximum_vote
if (candidates[i].votes > maximum_vote)
{
maximum_vote = candidates[i].votes;
}
}
//iterate over list of candidate
for (int i = 0; i < candidate_count; i++)
{
//check for candidate votes that are equal to maximum vote and print them as you go
if (candidates[i].votes == maximum_vote)
{
printf("%s\n", candidates[i].name);
}
}
return;
}
@7ossama7med
Copy link

thanks very much

@zoro-io
Copy link

zoro-io commented Mar 30, 2023

Damn, man. Thank you for posting this. The solution to print candidates who shared the maximum vote was quite elegant. I tried to store an array of winner indexes and store the winner or all candidates who tied in the array and print each candidates name who's index was stored in the array and it worked but was so damn confusing to debug. Simplicity is best.

Great job, man.

@nealxu0
Copy link

nealxu0 commented Aug 7, 2023

I hate myself. I thought candidate_count was initialized inside main and didn't realize candidate_count was initialized at the top so i spent so much time fighting check50 by using other methods to basically use candidate_count in a roundabout way until your post made me realize that. Thank you.

Copy link

ghost commented Aug 26, 2023

thank you so much for this

@Keniba1
Copy link

Keniba1 commented Oct 1, 2023

I write the same thing but when I run the code after every vote
it shows me invalid vote and don't show me the winner

@kingaslomin
Copy link

do you have "return true" after "candidates[i].votes++;" in the vote function?

@prophecy6
Copy link

it does that for me too I don't know how to fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment