0

I have tried a number of ways to execute the code in bool is_tie function; however, I always seem to receive the same error in check50.

I have run the code through debug50 and it seems to work correctly by displaying the names of the tied candidates after eliminating two others; however, in the check50 tool the above error continues to come up.

I have also used the printf function to display all of the values within the function and all of them seem to be correct as well.

I have also used else {istie = false;} in the below is_tie function I am having an issue with; however, I receive the same error below as I get with the code also below the error.

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

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    int ties = 0;
    bool istie = false;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min && candidates[i].eliminated == false)
        {
            ties++;
            int elim_cands = candidate_count - candidates_out;

            if (ties == candidate_count || ties == elim_cands)
            {
                istie = true;
                break;
            }
        }

    }
    return istie;
}

Similarily, I have been able to remove the error with this code immediately below; however, I only receive these two errors instead.

:( is_tie returns false when election is not tied
is_tie did not return false

:( is_tie returns false when only some of the candidates are tied
is_tie did not return false

 bool is_tie(int min)
{
    int ties = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min && candidates[i].eliminated == false)
        {
            ties++;
            return true;
        }
    }
    return false;
}                

If you have any ideas, please let me know what I can try. Thanks a lot!

All of the other functions are working correctly and the initial pset3 code is the same as provided. I did; however, add a global variable called candidates_out to keep track of the number of eliminated candidates.

**SPOLER ALERT - BELOW IS THE CODE FOR FUNCTIONS RELATED TO PROGRAM:

bool vote(int voter,int rank, string name) (void)tabulate(void) bool print_winner(void) bool is_tie(int min) int find_min(void) eliminate(int min)

   bool vote(int voter, int rank, string name)
   {
    for (int number = 0; number < candidate_count; number++)
    {
        if (strcmp(name, candidates[number].name) == 0)
        {
            preferences[voter][rank] = number;
            return true;
        }


    }
    return false;
}

    // Tabulate votes for non-eliminated candidates
    void tabulate(void)
{
    for (int j = 0; j < 1; j++)  //loop to record each voters rank 1
    {
        for (int i = 0; i < voter_count; i++)
        {
            int number = preferences[i][j];

            if (candidates[number].eliminated == false) //if candidate is not eliminated, add 1 to candidates votes
            {
                candidates[number].votes += 1;
                j = 0;
            }
            else                                        //if candidate is eliminated, go to the same voters next preference
            {
                i--;
                j++;
            }
        }

    }
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    float majority = (float)voter_count / 2;

    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > majority)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
    }
    return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    int min = candidates[0].votes;
    for (int i = 1; i < candidate_count; i++)
    {
        if (min > candidates[i].votes && candidates[i].eliminated == false)
        {
            min = candidates[i].votes;
            int Position = i;
        }
    }
    return min;
}


// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{

    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min)
        {
            candidates[i].eliminated = true;
            candidates_out++;
        }
    }

}

2 Answers 2

1

There's no need to count the candidates having min votes. If all the remaining candidates have min votes, it's a tie. Any candidate having more votes than min will tell you it's not a tie (yet).

1

The root of the problem:

add a global variable called candidates_out to keep track of the number of eliminated candidates.

From the spec:

you should not modify anything else in plurality.c

check50 uses it's own black magic to run tests and verify results. IMO one of the important skills learned in this class is following the spec to the letter. In a business setting, not doing so could translate to real money.

You must log in to answer this question.

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