Advertisement
Guest User

Untitled

a guest
May 23rd, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. bool vote(string name)
  2. {
  3.     for (int i = 0; i < candidate_count; i++)
  4.     {
  5.         //If candidate has been found in candidate array
  6.         if (strcmp(candidates[i].name, name) == 0)
  7.         {
  8.             candidates[i].votes++;
  9.             return true;
  10.         }
  11.     }
  12.     //No candidate found
  13.     return false;
  14. }
  15.  
  16. // Print the winner (or winners) of the election
  17. void print_winner(void)
  18. {
  19.     int leadertally = 0;
  20.  
  21.     //get the highest vote
  22.     for (int i = 0; i < candidate_count; i++)
  23.     {
  24.         if (candidates[i].votes > leadertally)
  25.         {
  26.             leadertally += candidates[i].votes;
  27.         }
  28.     }
  29.     //print the highest candidate(s)
  30.     for (int j = 0; j < candidate_count; j++)
  31.     {
  32.         if (candidates[j].votes == leadertally)
  33.         {
  34.             printf("%s\n", candidates[j].name);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement