I've been trying to resolve these myself for too long - I'm officially stuck

These are the check50 errors I get:

:( tabulate counts votes when multiple candidates are eliminated

:( find_min ignores eliminated candidates

:( is_tie detects tie after some candidates have been eliminated

All of them seem to have something to do with how I check if candidates are eliminated. My logic is pretty-straightforward - if candidates[preferences[i][0]].eliminated == false, or (candidates[k].eliminated == false), or (candidates[i].eliminated == false), then do what's needed and move on to the next for loop. If no, then do nothing and move on to the next for loop

Can someone please point me in the right direction? Thanks!

As I was writing this I realized I've probably overcomplicated the find_min function but still it'd be great to understand why it doesn't work as I intended

PS Please ignore all my printf's - that's just me trying to figure out what's going on

Here's the code:


void tabulate(void)
    for (int i = 0; i < voter_count; i++)
        // Check if voter's top choice candidate hasn't been eliminated from the election
        if (candidates[preferences[i][0]].eliminated == false)
            // Update the number of votes
            //printf("%s - %i (%i)\n", candidates[preferences[i][0]].name, candidates[preferences[i][0]].votes, candidates[preferences[i][0]].eliminated);
            //printf("%s - %i (%i)\n", candidates[preferences[i][1]].name, candidates[preferences[i][1]].votes, candidates[preferences[i][1]].eliminated);


int find_min(void)
    for (int i = 0; i < candidate_count - 1; i++)
        for (int k = 0; k < candidate_count - 1; k++)
            // Check if a candidate hasn't been eliminated from the election
            if (candidates[k].eliminated == false)
                //  Check if a candidate's votes number is larger then of the following one
                if (candidates[k].votes > candidates[k + 1].votes)
                    // Swap the votes numbers if it is larger
                    int votes = candidates[k].votes;
                    candidates[k].votes = candidates[k + 1].votes;
                    candidates[k + 1].votes = votes;

                    // Same goes for names
                    string name = candidates[k].name;
                    candidates[k].name = candidates[k + 1].name;
                    candidates[k + 1].name = name;

                    // And eliminated bool
                    bool eliminated = candidates[k].eliminated;
                    candidates[k].eliminated = candidates[k + 1].eliminated;
                    candidates[k + 1].eliminated = eliminated;

    // Define the variable with the min votes number
    int min = candidates[0].votes;
    //printf("%i\n", min);
    return min;
    //return 0;


bool is_tie(int min)
    int j = 0;
    for (int i = 0; i < candidate_count; i++)
            //printf("Candidates - %i\n", candidate_count);
            //printf("min - %i\n", min);
            //printf("%s - %i (%i)\n", candidates[i].name, candidates[i].votes, candidates[i].eliminated);
            //printf("%s - %i (%i)\n", candidates[i + 1].name, candidates[i + 1].votes, candidates[i + 1].eliminated);
            //printf("%s - %i (%i)\n", candidates[i + 2].name, candidates[i + 2].votes, candidates[i + 2].eliminated);
            // Check if a candidate hasn't been eliminated from the election
            if (candidates[i].eliminated == false)
                // Check if votes number is equal to min
                if (candidates[i].votes == min)
    if (j == candidate_count)
        return true;
    return false;

4 Answers 4


tabulate: What if the voter's top candidate is eliminated? Your code will never check their next preference, but it needs to do that.

find_min: it looks like you're trying to sort the candidates array, but you don't need to do that. You can go through the array once, keep track of what the minimum is so far, and if the next vote count is lower, that's the new minimum. This is O(n), where sorting by the bubble sort is O(n^2), so this is a much more efficient algorithm.

You're not actually sorting the candidates array, because you skip sorting if candidates[k] has been eliminated, though. You don't check if candidates[k+1] is eliminated, so it's very possible that candidates[0] will be an eliminated candidate at the end. If you're going to sort, you need to remove that eliminated check when you sort, then go through and find the first candidate that isn't eliminated.

is_tie: You're counting the number of candidates that have a vote count equal to the minimum, and if every candidate has that vote count, you're returning true. But think about if there are three candidates, Alice and Bob have 4 votes, and Charlie is eliminated and so has 0 votes. Your algorithm will have j = 2 at the end, and candidate_count = 3, so it will return false, but should return true. You can check if j = the_number_of_not_elimated_candidates. (Don't update candidate_count, because then you'll mess up all your loops in the next passes through!)

Alternately, you can flip your reasoning. All non-eliminated candidates having vote counts equal to the min means candidates[i].votes != min will never be true when candidates[i] hasn't been eliminated. You can switch to checking that, and if someone's votes aren't equal to the min, return false immediately.

General comment: you can replace candidates[i].eliminated == false with !candidates[i].eliminated, which would be a more common way of writing that.



You first need to store voter's top preferred Candidate by copying the candidate number fromperferences[i][0] and store it in int = n. Now you have to increase the vote count of the same candidate by candidates[n].vote++



You first need to tell the computer about the first vote because it will help the computer to compare the first vote with others.

int n = candidates[0].vote

Now simple algorithm, if next vote is smaller than n than update n, otherwise move to next one and check till all the candidates have been passed.

Be sure to check the votes of the candidates who are still not eliminated



Now you have calculated min, check by every candidate who is .eliminated == false if his vote == min, it should return true till all the candidates have been checked. Otherwise return false

You must log in to answer this question.

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