0

I have managed to complete the plurality problem for problem set 3, however when I run check50 I get this one error:

:( print_winner identifies Bob as winner of election print_winner function did not print winner of election

I am confused as to why I am getting this error as I am not getting errors for identifying Charlie or Alice as winners of the election. Here is my code for the print_winner function:

    void print_winner(void)
    {
        //create an array called candidates_votes
        int candidates_votes[MAX] = {};

        //append this array with all the candidate votes
        for (int i = 0; i < candidate_count; i++)
        {
            candidates_votes[i] = candidates[i].votes;
        }

        //get highest value in array
        int max = candidates_votes[0];

        for (int i = 1; i < candidate_count; i++)
        {
            if (candidates_votes[i] >= candidates_votes[0])
            {
                max = candidates_votes[i];
            }
        }

        //comparing candidate votes to this max value
        for (int i = 0; i < candidate_count; i++)
        {
            if (candidates[i].votes == max)
            {
                printf("%s\n", candidates[i].name);
            }
        }
    }
0

you don't need another array, the main function has saved votes count to the struct and you have to compare each candidate votes count with current "max" not initial max

| improve this answer | |

You must log in to answer this question.

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