0

My code compiles but the only functions that had no errors are the print_winner and eliminate functions. I have tried to solve the errors for the other functions with no luck.

My errors with the vote function are: ":( vote returns false when given name of invalid candidate,vote function did not return false", ":( vote correctly sets first preference for first voter, vote function did not correctly set preferences", and ":( vote correctly sets all preferences for voter, vote function did not correctly set preferences".

For my tabulate function: ":( tabulate counts votes when all candidates remain in election, tabulate function did not produce correct vote totals", ":( tabulate counts votes when one candidate is eliminated, tabulate function did not produce correct vote totals", and ":( tabulate counts votes when multiple candidates are eliminated, tabulate function did not produce correct vote totals".

For my find_min function: ":( find_min returns minimum number of votes for candidate, find_min did not identify correct minimum", and ":( find_min returns minimum when all candidates are tied, find_min did not identify correct minimum".

Lasltly, my is_tie function: ":( is_tie detects tie after some candidates have been eliminated, is_tie did not return true".

**
bool vote(int voter, int rank, string name)
{
    // iterates over candidates to see if name matches the name provided for ranking and if so records the preference
    for (int i = 0; i < candidate_count; i++)
    {
             if (strcmp(name, candidates[i].name) == 0)
             {
                  preferences[voter][rank] = i;
                  return true;
             }
    }
    return false;
}

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

// Print the winner of the election, if there is one
bool print_winner(void)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {

        if (candidates[i].votes > voter_count / 2 + 0.5)
        {
        printf(" %s\n", candidates[i].name);
        return true;
        }
    }
    return false;
}

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

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    // TODO
    int tiecount = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == min)
        {
            tiecount++;
        }
        if (candidates[i].eliminated == false && tiecount == candidate_count)
        {
            return true;
        }
    }
    return false;
}

// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].eliminated == false && candidates[i].votes == min)
        {
            candidates[i].eliminated = true;
        }
    }
    return;
}
**

Any help would be greatly appreciated!

  • tabulate: you never actually use the preferences array in your code. I'd look into where/how you could set your preferences for all your voters. find_min: look at your if statement. don't forget that when a function returns it stops in it's tracks. so if you have 4 candidates and at first pass their vote totals are 5 3 4 2. You're code would cycle through and see when i = 1 that the vote is lower, and it returns thinking your function is complete. tie_count: you're incrementing your tie_count variable based on what's == min. if two candidates have the min vote does that = a tie? – Sean Apr 15 at 20:35
  • Thanks @sean, that was a big help! – ayeeitsalpha Apr 18 at 23:23
0

Thanks to seans help I was able to figure out the issue, here is my new code:

bool vote(int voter, int rank, string name)
{
    // iterates over candidates to see if name matches the name provided for ranking and if so records the preference
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(name, candidates[i].name) == 0)
        {
            preferences[voter][rank] = i;
            return true;
        }
    }
    return false;
}

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

// Print the winner of the election, if there is one
bool print_winner(void)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {

        if (candidates[i].votes > voter_count / 2 + 0.5)
        {
            printf(" %s\n", candidates[i].name);
            return true;
        }
    }
    return false;
}

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

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    // TODO
    int tiecount = 0;
    int remaining = 0;
    bool isTie = false;
    for (int i = 0; i < candidate_count; i++)
    {
        if (!candidates[i].eliminated)
        {
            remaining++;
        }
        if (candidates[i].votes == min)
        {
            tiecount++;
        }
        if (tiecount == remaining)
        {
            isTie = true;
        }
        if (tiecount != remaining)
        {
            isTie = false;
        }
    }
    return isTie;
}

// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].eliminated == false && candidates[i].votes == min)
        {
            candidates[i].eliminated = true;
        }
    }
    return;
}
| improve this answer | |

You must log in to answer this question.

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