0

I cannot figure out why I am getting rejected on check50 for ":( tabulate handles multiple rounds of preferences - tabulate function did not produce correct vote totals". I get green successful marks on all other checks, including ":) tabulate counts votes when multiple candidates are eliminated".

Below is my tabulate function. Any help to point me in the right direction is appreciated.

void tabulate(void)
{

int vote_num = 0;
int rank = 0;


for (int i = 0; i < voter_count; i++)
{
    vote_num = preferences[i][rank];

    if (candidates[vote_num].eliminated == false)
    {
    candidates[vote_num].votes++;
    }
    else
    {
        for (rank = 0; rank < candidate_count; rank++)
        {
            if (candidates[preferences[i][rank]].eliminated == false)
            {
            candidates[preferences[i][rank]].votes++;
            break;
            }

        }
    }

}

return ;

}
0

I would take a look at the logic behind vote_num because I'm not sure if that is working how you intend it to. You initiate it to one, never increment it, and use it to access a candidate in

if (candidates[vote_num].eliminated == false)
{
    candidates[vote_num].votes++;
}

to determine if your function should run the above code or the else statement. Now, I'm not sure how you are treating your candidates array elsewhere in your code, but this snippet makes it look like you are always incrementing the vote for the first candidate in the array by 1 each time you tabulate votes, independent of whether they have votes. This function isn't passed a parameter, so I am assuming that this candidates array is what is storing each voter's preferences (though you have a preferences array that would seem to do that)?

This is my guess given the code, but without comments in the function to explain how everything relates back to the program, it is hard to draw a firm conclusion. Hopefully, this helps.

0

modify your code to this

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++;
                break;
            }
        }
    }
    return;
}

You must log in to answer this question.

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