0

This is my print_winner function

void print_winner(void)
{
    int winner_vote;

    for (int i = 0; i < candidate_count; i++)
    {
        for(int j = i + 1; j < candidate_count; j++)
        {
            if(candidates[i].votes < candidates[j].votes)
            {
                winner_vote = candidates[j].votes;
            }
            else
            {
                winner_vote = candidates[i].votes;
            }
        }
    }

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

    return;
}

My intention with the code- suppose there are 5 candidates- a,b,c,d,e. What I'm trying to do is first compare a to all the others & then b with all the others except a & the c with all the others except a & b & so on.

& this is the only error I got- print_winner did not print Alice as winner. But the thing is it passed every other case in check50. It printed all the single winner(except Alice) & all the winners if there was a tie. So then why isn't this code working for the Alice case?

I also found the following code which passed check50-

void print_winner(void)
{
    int winner_vote;

    for (int i = 0; i < candidate_count; i++)
    {
        for(int j = 0; j < candidate_count -i; j++)
        {
            if(candidates[i].votes < candidates[j].votes)
            {
                winner_vote = candidates[j].votes;
            }
            else
            {
                winner_vote = candidates[i].votes;
            }
        }
    }

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

    return;
}

& I don't understand how the 2nd one is working.

So my questions are these-

  1. Why is my code wrong? Why is it giving error in one of the single winner case when it passed all the other cases?

  2. Why is the 2nd code correct?

PS- I found the 2nd case by trial & error.

2
  • Both are the same. Are you sure you pasted the correct code?
    – Vsjain
    Jan 24 at 11:16
  • @Vsjain Thank you I edited the 2nd one. Jan 24 at 11:50
1

Try an election with 3 candidates, A B C, and 6 voters, with the result A 3, B 1, C 2. The self-evident winner is A with 3 votes. The first print_winner function will return C. On the first iteration of i, winner_votes will be 3. However, on the second iteration, where i equals 1, it will only compare B to C, and winner_votes will be 2.

Since the inner (j) loop in the 2nd function starts with 0 (A), it will calculate winner_votes as 3.

Perhaps think about how program could loop through the candidates exactly once and calculate the "max" winner_votes.

1
  • I was finally able to think of a program that could loop through the candidates exactly once & calculate the max winner_votes. Thank you! Jan 25 at 4:01

You must log in to answer this question.

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