• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1028
    admin
    Keymaster
      
      
      // Check if election has been won
              bool won = print_winner();
              if (won)
              {
                  break;
              }

      An explanation of the above code will be helpful.

      Here is the print_winner function that I have coded:

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

       

      As far I understand, a variable won is created that is of bool type. The value of won will be equal to what is returned by the print_winner function.

      The print_winner function will break (not sure if terminate will be the right term here) if indeed there is a winner (line 17 and 18 of print_winner function). I understand after break on line 18, the function print_winner will terminate but not the whole program. In case break applicable, the result which is true passed on to the value of won?

      Not clear what it means by

      
      
      // Check if election has been won
              bool won = print_winner();
              if (won)
              {
                  break;
              }

       


      Reply

      https://edstem.org/us/courses/176/discussion/2378898?answer=5427665[learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top