I've been trying to resolve these myself for too long - I'm officially stuck
These are the check50 errors I get:
:( tabulate counts votes when multiple candidates are eliminated
:( find_min ignores eliminated candidates
:( is_tie detects tie after some candidates have been eliminated
All of them seem to have something to do with how I check if candidates are eliminated. My logic is pretty-straightforward - if candidates[preferences[i][0]].eliminated == false
, or (candidates[k].eliminated == false)
, or (candidates[i].eliminated == false)
, then do what's needed and move on to the next for loop. If no, then do nothing and move on to the next for loop
Can someone please point me in the right direction? Thanks!
As I was writing this I realized I've probably overcomplicated the find_min function but still it'd be great to understand why it doesn't work as I intended
PS Please ignore all my printf's - that's just me trying to figure out what's going on
Here's the code:
tabulate
void tabulate(void)
{
for (int i = 0; i < voter_count; i++)
{
// Check if voter's top choice candidate hasn't been eliminated from the election
if (candidates[preferences[i][0]].eliminated == false)
{
// Update the number of votes
candidates[preferences[i][0]].votes++;
//printf("%s - %i (%i)\n", candidates[preferences[i][0]].name, candidates[preferences[i][0]].votes, candidates[preferences[i][0]].eliminated);
//printf("and\n");
//printf("%s - %i (%i)\n", candidates[preferences[i][1]].name, candidates[preferences[i][1]].votes, candidates[preferences[i][1]].eliminated);
//printf("\n");
}
}
return;
}
find_min
int find_min(void)
{
for (int i = 0; i < candidate_count - 1; i++)
{
for (int k = 0; k < candidate_count - 1; k++)
{
// Check if a candidate hasn't been eliminated from the election
if (candidates[k].eliminated == false)
{
// Check if a candidate's votes number is larger then of the following one
if (candidates[k].votes > candidates[k + 1].votes)
{
// Swap the votes numbers if it is larger
int votes = candidates[k].votes;
candidates[k].votes = candidates[k + 1].votes;
candidates[k + 1].votes = votes;
// Same goes for names
string name = candidates[k].name;
candidates[k].name = candidates[k + 1].name;
candidates[k + 1].name = name;
// And eliminated bool
bool eliminated = candidates[k].eliminated;
candidates[k].eliminated = candidates[k + 1].eliminated;
candidates[k + 1].eliminated = eliminated;
}
}
}
}
// Define the variable with the min votes number
int min = candidates[0].votes;
//printf("%i\n", min);
return min;
//return 0;
}
is_tie
bool is_tie(int min)
{
int j = 0;
for (int i = 0; i < candidate_count; i++)
{
//printf("Candidates - %i\n", candidate_count);
//printf("min - %i\n", min);
//printf("%s - %i (%i)\n", candidates[i].name, candidates[i].votes, candidates[i].eliminated);
//printf("%s - %i (%i)\n", candidates[i + 1].name, candidates[i + 1].votes, candidates[i + 1].eliminated);
//printf("%s - %i (%i)\n", candidates[i + 2].name, candidates[i + 2].votes, candidates[i + 2].eliminated);
// Check if a candidate hasn't been eliminated from the election
if (candidates[i].eliminated == false)
{
// Check if votes number is equal to min
if (candidates[i].votes == min)
{
j++;
}
}
}
if (j == candidate_count)
{
return true;
}
else
return false;
}