1

I'm having a hard time with the runoff problem in pset3, especially with the tabulate function. Based on the instructions it looks like I need to update the preferences array but I'm not sure how. I'm also getting check50 errors with the printwinner function and am not sure what I'm doing wrong there... Any advice is appreciated!

// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
    for (int i = 0; i < candidate_count; i++)  // Step through the candidate array
    {
        if (strcmp(candidates[i].name, name) == 0) // See if the candidate name matches the name the voter provided
        {
            preferences[voter][rank] = i;
            return true;
        }
    }
return false;
}

// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    for (int k = 0; k < voter_count; k++)
    {
        for (int f = 0; f < candidate_count; f++)
        {
            if (candidates[f].eliminated == false)
            {
                candidates[f].votes++;
            }
        }
    }
    return;
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > (voter_count / 2))
        {
            printf("%s", candidates[i].name);
            return true;
        }
    }
    return false;
}

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

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min) // TODO revisit this I'm confused why this mostly works
{
    for (int d = 0; d < candidate_count; d++)
    {
        if (candidates[d].votes == candidates[d - 1].votes)
        {
            return true;
        }
        if (candidates[d].votes == candidates[d - 1].votes)
        {
            return false;
        }
    }
    return false;
}

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

Remember that not always diving integers will yield also integers.

So, how can you overcome this problem?

Well

Dividing a integer by an interger will sometimes yield float (decimal notation)

So you need to store that value inside a float

float = (formula for more than half of the votes);

And now

int number = round(float_value);

And remember to add math.h for round

| improve this answer | |
  • thanks! This helped, and then I had to add \n to create a new line after printing the winner's name. Without that it wouldn't pass check50! – getsendy Jun 27 at 20:54
1

Just got tabulate to work with this code!

void tabulate(void)
{
    for (int k = 0; k < voter_count; k++)
    {
        if (candidates[preferences[k][0]].eliminated == false)
        {
            candidates[preferences[k][0]].votes++;
        }
        else if (candidates[preferences[k][0]].eliminated == true)
        {
            candidates[preferences[k][1]].votes++;
        }
    }
    return;
}
| improve this answer | |
0

Think about a second how tabulate() function works.

You should only update the votes of the candidates who is only given first rank or preference

And how can you access the first preference

preferences[changing row][first column only]

Inside preferences there is nothing just a number (of the candidate) or integers.

Remember

update the vote of the candidate[i] where i is int

| improve this answer | |
  • so helpful, thank you! – getsendy Jun 27 at 17:25
  • Thanks mate :) if I helped you be sure to tick the answer – Shahroz Ali Jun 27 at 17:29
  • Done! Also, do you have any advice on the printwinner function? I'm very flummoxed on why that one isn't working. It seems like printing someone who has more than half the vote would work there? – getsendy Jun 27 at 18:50
  • Yeah! Sure. Your print_winner function should print the name of the candidate who has more than half of the majority of the votes. – Shahroz Ali Jun 27 at 18:53
  • Yes check answer in the answer section. – Shahroz Ali Jun 27 at 18:59
0

Still getting errors on find_min and is_tie:

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

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    int p = 0; // int for total of non-eliminated candidates
    for (int d = 0; d < candidate_count; d++)
    {
        if (candidates[d].eliminated == false) // find number of non-eliminated candidates
        {
            p++;
        }
    }
    for (int d = 0; d < candidate_count; d++)
    {
        if (candidates[d].eliminated == false)
        {
            float r = (voter_count / p);  // votes if all non-eliminated candidates are tied
            int t = roundf(r);
            for (int y = 0; y < p; y++)
            {
                if (candidates[y].votes == t)
                {
                    return true;
                }
            }
        }
    }
    return false;
}
| improve this answer | |
0

find_min Remember your function should return minimum number of votes after checking all the candidates.

return min;

And how could you assign minimum number of votes? Well,

Think about this,

You first need to assign the lowest possible number already to int min; so that it can be used to check that if candidates[i].votes =< min you should store the value in int min; and check again and check again till all the candidates have been checked and return min;

And how could you assign the lowest number of votes?

min = candidates[first_candidate].votes;
| improve this answer | |

You must log in to answer this question.

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