0

I'm working on the tabulate() function, and to an extent the is_tie() function

The tabulate() function passes all the tests of check50 but the last one:

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

I know I have to somehow code that if a voter has chosen (in 1st choice) a specific candidate but that candidate has been already eliminated, we have to repeat the function by looking at his next best choice (in 2nd choice), and so on until we find he/she has voted for a candidate that's still running for the election.

In my mind the logic makes sense as it is, but it still reports an error. Why? Help please!

On the other hand, I get a similar error with is_tie():

:( is_tie detects tie after some candidates have been eliminated
    is_tie did not return true

But I feel that if I get the logic of the first right it'll be easy to solve this second one.

This is my attempt at the tabulate() function.

void tabulate(void)
{
    int r = 0;  // Rank variable initially set to 0 to look initially at the voter's first preference.

    for (int k = 0; k < voter_count; k++) // Repeat this process for each voter in the election.
    {
        for (int i = 0; i < candidate_count; i++)   // Check each candidate in the ballot.
        {
            if(preferences[k][r] == i && !candidates[i].eliminated)     // If a voter chose candidate 'i' as their 'r'st choice (starting from 1st choice), 
                                                                        // provided that the candidate was not eliminated, add one vote to him/her and stop the function.
            {
                candidates[i].votes++;
                break;

            }

            if(candidates[i].eliminated && preferences[k][r] == i)      // If a voter chose candidate i as their 'r'st choice (starting from 1st choice) but the candidate
                                                                        // was eliminated, repeat the function looking at his next preferred choice (2nd choice and so on). 
            {
                r++;
            }
        }
    }   

    return;      
}

Many thanks in advance!

1 Answer 1

1

I solved it myself with the debugger!

There was a problem with the indices of my loops when going down one level of choice for a voter (i.e. when a first choice was an eliminated candidate).

A simple variable re-assignment to 'i' so that the loop restarts at i = 0 on the next iteration solved it.

You must log in to answer this question.

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