0

My program behave correctly when i try it manually but it fails in check50. It returns " :( print_winner identifies Alice as winner of election " print_winner function did not print winner of election.

// Print the winner (or winners) of the election
void print_winner(void)
{

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

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

    return;


}

Thanks in advance for any help.

  • You can remove your else in the first for loop it's redundant. since you iterate over all the candidates, and if they are not greater than max you set the next index as max then after that you go on to the next loop to see if the next index the index you just set as max if it is greater than max. – Ojou Nii May 21 at 1:41
0

In your first if statement you're comparing adjacent pairs in the array instead of max. Say, if x, y, and z are the candidates with 6,5,4 votes, respectively. When the array gets to comparing Y and Z, it will determine that Y is the max score.

| improve this answer | |
  • It's ok i change my if statement by adding candidates[i].votes >= max. Thank you – Aymene Cherradi May 21 at 23:50

You must log in to answer this question.

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