Advertisement
bocajbee

plurality.c

Feb 8th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX 9  // max number of candidates
  6.  
  7. typedef struct  // candidates have name and vote count
  8. {
  9.     string name;
  10.     int votes;
  11. }
  12. candidate;
  13.  
  14. candidate candidates[MAX];  // array of candidates
  15.  
  16. int candidate_count;  // number of candidates
  17.  
  18. bool vote(string name);  // function prototypes
  19. void print_winner(void);
  20.  
  21. int main(int argc, string argv[])
  22. {
  23.     if (argc < 2)  // check for invalid usage
  24.     {
  25.         printf("Usage: plurality [candidate ...]\n");
  26.         return 1;
  27.     }
  28.  
  29.     candidate_count = argc - 1;  // populate array of candidates
  30.     if (candidate_count > MAX)
  31.     {
  32.         printf("Maximum number of candidates is %i\n", MAX);
  33.         return 2;
  34.     }
  35.     for (int i = 0; i < candidate_count; i++)
  36.     {
  37.         candidates[i].name = argv[i + 1];
  38.         candidates[i].votes = 0;
  39.     }
  40.  
  41.     int voter_count = get_int("Number of voters: ");
  42.  
  43.     for (int i = 0; i < voter_count; i++)  // loop over all voters
  44.     {
  45.         string name = get_string("Vote: ");
  46.  
  47.         if (!vote(name))  // check if vote() returns FALSE, if so...
  48.         {
  49.             printf("Invalid vote.\n");
  50.         }
  51.     }
  52.     print_winner(); // display winner of election
  53. }
  54.  
  55. bool vote(string name)  // update vote totals given a new vote
  56. {
  57.     for (int i = 0; i < candidate_count; i++)   // iterate through each of the candidates[] one by one
  58.     {
  59.         if (strcmp(name, candidates[i].name) == 0)  // compare the strings "name" and candidate[i].name. IF they match...
  60.         {
  61.             candidates[i].votes++;  // add a value of 1 to the candidates votes
  62.             return true;  // return TRUE, indicating the vote is valid
  63.         }
  64.     }
  65.     return false;  // else return false to where this functionis being called
  66. }
  67.  
  68. void print_winner(void)  // print winner of election
  69. {
  70.     int maxvote = 0;  // integer vairable in memory to store the value of the highest amount of votes that will be collected
  71.  
  72.     for (int i = 0; i < candidate_count; i++)  // loop through each of the candidates[]
  73.     {
  74.         if (candidates[i].votes >= maxvote)
  75.         {
  76.             maxvote = candidates[i].votes;
  77.         }
  78.     }
  79.     for (int i = 0; i < candidate_count; i++)  // loop through each of the candidates[] (again)
  80.     {
  81.         if (candidates[i].votes == maxvote) // if the "ith" candaidates vote count == the maxvote variable
  82.         {
  83.             printf("%s WINNER: ", candidates[i].name);  // print the name of the candidate at this index
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement