0

I'm stuck on tabulate function, my code didn't pass any of the requirements in check50.

Requirements:

  1. tabulate counts votes when all candidates remain in election
  2. tabulate counts votes when one candidate is eliminated
  3. tabulate counts votes when multiple candidates are eliminated
  4. tabulate handles multiple rounds of preference

I wrote nested for loops to loop through every voter's first choice, if their first choice match one of the candidates and that candidate is not eliminated then add one, if it matched but the candidate was eliminated, then check the next candidate. I have no clue how to approach this problem right now. I tried to use the debugger, but I'm only able to see the local variable. Also, is the returnnecessary here when the function has a void return type?

void tabulate(void)
{
    for (int i = 0; i < voter_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] == j && !(candidates[j].eliminated))
            {
               candidates[j].votes++;
            }
        }
    }
    return;
}
1
  • Understand what preferences array is. Each row indicates a voter. Each column indicates voter's preference/ rank of a candidate. The value inside is candidate number. preferences[i][j] will not be equal to j, since j is voter's rank for a candidate & prefrences[i][j] is candidate number – C-- Mar 29 at 6:46

You must log in to answer this question.

Browse other questions tagged .