Objective: Print the name/s with the highest votes
So I was debugging my PSET3 Plurality for probably 8+ hours now. I thought everything was fixed since the result in the debugger was what I intended, but, in my integrated terminal, it produced the result differently.
Debugger result:
plurality/ $ debug50 plurality ./plurality me duck you
.
Number of voters: 7
Vote: me
Vote: me
Vote: me
Vote: d
Invalid vote.
Vote: duck
Vote: duck
Vote: you
me
Integrated Terminal result:
plurality/ $ ./plurality me duck you
Number of voters: 7
Vote: me
Vote: me
Vote: me
Vote: d
Invalid vote.
Vote: duck
Vote: duck
Vote: you
duck
plurality/ $
"me" should win right, but why did "duck" did?
My Code(I thought of making a minimal reproducible example, but I don't know how to, that's the most minimal I could think of, I'm sorry. Just scroll all the way down.):
#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;
//Swapping Function
void swap (char ** xptr, char ** yptr);
// 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)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
candidates[i].votes += 1;
return true;
}
}
return false;
}
This section might help you...
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
for (int z = 0; z < candidate_count - 1; z++)
{
int n = 0;
while (n < candidate_count - 2)
{
int a = candidates[n+1].votes;
int b = candidates[n+2].votes;
char *x = candidates[n+1].name; //me
char *y = candidates[n+2].name; //duck
if (a > b)
{
a = a + b; //Swapping techniques credits to GeekforGreeks.org & Childre'sTechVideo
b = a - b;
a = a - b;
swap(&x, &y);
}
candidates[n+1].votes = a;
candidates[n+2].votes = b;
candidates[n+1].name = x;
candidates[n+2].name = y;
n++;
}
}
int c = 0;
while (c != candidate_count - 1)
{
c++;
if (candidates[candidate_count-1].votes == candidates[c].votes)
{
printf("%s\n", candidates[c].name);
}
}
}
//Swap function
void swap (char ** xptr, char ** yptr)
{
char * tmp = NULL;
tmp = *xptr;
*xptr = *yptr;
*yptr = tmp;
}