0

when I run check50 this is the message I get and I don't know what to fix

:( tabulate counts votes when all candidates remain in election
 tabulate function did not produce correct vote totals

:( tabulate counts votes when one candidate is eliminated
 tabulate function did not produce correct vote totals

:( tabulate counts votes when multiple candidates are eliminated
 tabulate function did not produce correct vote totals

this is the code

bool vote(int voter, int rank, string name)
{
for (int a = 0; a < candidate_count; a ++)
{
    if (strcmp(candidates[a].name, name) == 0)
    {
        preferences[voter][rank] = a;//a is the location of the candidate
        return true;
    }
}
return false;
}

// Tabulate votes for non-eliminated candidates
void tabulate(void)
{

for (int i = 0; i < voter_count; i ++)
{
    for (int j = 0; j < candidate_count; j ++)
    {
        if (candidates[preferences[i][j]].eliminated == false)
        {
            candidates[preferences[i][j]].votes ++;
        }
        /*else
        {
            candidates[preferences[j][k+1]].votes ++;
        }*/
    }
}
return;
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
for (int i = 0; i < candidate_count; i ++)
{
    if (candidates[i].votes > (voter_count/2))
    {
        printf("%s\n", candidates[i].name);
        return true;
    }
}
return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
int min = voter_count;
for (int i = 0; i < candidate_count; i ++)
{
    if ((candidates[i].eliminated == false) && (min > candidates[i].votes))
    {
        min = candidates[i].votes;
    }
}
return 0;
}

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
for (int i = 0; i < candidate_count; i ++)
{
    if (candidates[i].eliminated == false && candidates[i].votes != min)
    {
        return false;
    }
}
return true;
}

// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
for (int k = 0; k < candidate_count; k ++)
{
    if (candidates[k].votes == min)
    {
        candidates[k].eliminated = true;
    }
}
return;
}
0

I'm new here but I solved this problem set a while back and maybe I can help with this.

If I run debug50 on your code as follows: ./runoff alice charlie bob and number of voters: 3 and try to print out each individual's count and name in the end, it shows me this:

3, alice 3, bob 3, charlie

It seems like for each voter every non-eliminated candidate gets a vote, no matter who their actual preference is. Meaning, it starts at the first voter and loops through j < candidate_count and gives each candidate a vote, only then does it jump to the next voter and the whole thing repeats.

You have to figure out how to only give the voters first choice etc. a vote, as long as their not eliminated and then move to the next voter. It's a minor thing. An option would be to, once you add the vote to that specific candidate, stop the loop so that it can go to the next voter and so on. That will also make the else argument unnecessary.

Hope this is understandable and helpful!

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .