0

I am stuck at this error for hours, I would appreciate your help. There is two errors:

:) tabulate counts votes when all candidates remain in election

:) tabulate counts votes when one candidate is eliminated

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

:( tabulate handles multiple rounds of preferences tabulate function did not produce correct vote totals

The first one I checked and it did produce the correct vote totals and I do not understand what the second one means. Here is my code:

    // Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    for (int i = 0; i < voter_count; i++)
    {
        int j = 0;
        for (int z = 0; z < candidate_count; z++)
        {
            if (preferences[i][j] == z)
            {
                if (candidates[preferences[i][j]].eliminated == true)
                {
                    (j++);
                }
                else
                {
                    (candidates[preferences[i][j]].votes++);
                    break;
                }
            }
        }
    }
    return;
}

1 Answer 1

0

Try this election with 4 candidates (a b c d) and 5 voters:

voter pref 1 pref 2 pref 3 pref 4
1 d c b a
2 a b c d
3 a b c d
4 b c a d
5 b c a d

b should be the winner with 3 votes.

The problem is the z loop in candidate order. Votes need to be awarded in preference order. In the 3rd round, after c and d are eliminated, voter 0's remaining top preference (b) will not be counted. j (preference) is not incremented until z is 3 (candidate d), but the z loop is over, and the routine moves on to the next voter.

You must log in to answer this question.

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