Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. // Max number of candidates
  7. #define MAX 9
  8.  
  9. // Candidates have name and vote count
  10. typedef struct
  11. {
  12.     string name;
  13.     int votes;
  14. }
  15. candidate;
  16.  
  17. // Array of candidates
  18. candidate candidates[MAX];
  19.  
  20.  
  21. // Number of candidates
  22. int candidate_count;
  23.  
  24. // Function prototypes
  25. bool vote(string name);
  26. void print_winner(void);
  27.  
  28. int main(int argc, string argv[])
  29. {
  30.     // Check for invalid usage
  31.     if (argc < 2)
  32.     {
  33.         printf("Usage: plurality [candidate ...]\n");
  34.         return 1;
  35.     }
  36.  
  37.     // Populate array of candidates
  38.     candidate_count = argc - 1;
  39.     if (candidate_count > MAX)
  40.     {
  41.         printf("Maximum number of candidates is %i\n", MAX);
  42.         return 2;
  43.     }
  44.     for (int i = 0; i < candidate_count; i++)
  45.     {
  46.         candidates[i].name = argv[i + 1];
  47.         candidates[i].votes = 0;
  48.     }
  49.  
  50.     int voter_count = get_int("Number of voters: ");
  51.  
  52.     // Loop over all voters
  53.     for (int i = 0; i < voter_count; i++)
  54.     {
  55.         string name = get_string("Vote: ");
  56.  
  57.         // Check for invalid vote
  58.         if (!vote(name))
  59.         {
  60.             printf("Invalid vote.\n");
  61.         }
  62.     }
  63.  
  64.     // Display winner of election
  65.     print_winner();
  66. }
  67.  
  68. // Update vote totals given a new vote
  69. bool vote(string name)
  70. {
  71.     int y;
  72.     bool exist = false;
  73.     for (y = 0; y < candidate_count; y++)
  74.     {
  75.         if (strcmp(name, candidates[y].name) == 0)
  76.         {
  77.             exist = true;
  78.             candidates[y].votes += 1;
  79.             return true;
  80.            
  81.         }
  82.        
  83.        
  84.     }
  85.     return false;
  86.    
  87.    
  88.    
  89. }
  90.  
  91. // Print the winner (or winners) of the election
  92. void print_winner(void)
  93. {
  94.  
  95.     int swap_counter = -1;
  96.  
  97.     string winning_candidate = "";
  98.  
  99.     int winning_votes;
  100.  
  101.     if (swap_counter != 0)
  102.  
  103.     {
  104.  
  105.         for (int i = candidate_count; i > 0; i--)
  106.  
  107.         {
  108.  
  109.             if (candidates[i].votes < candidates[i - 1].votes)
  110.  
  111.             {
  112.  
  113.                 swap_counter = 0;
  114.  
  115.                 candidates[i] = candidates[i - 1];
  116.  
  117.                 candidates[i - 1] = candidates[i];
  118.  
  119.                 winning_votes = candidates[i].votes;
  120.  
  121.                 swap_counter++;
  122.  
  123.             }
  124.  
  125.         }
  126.  
  127.     }
  128.  
  129.     for (int i = 0; i < candidate_count; i++)
  130.  
  131.     {
  132.  
  133.         if (winning_votes == candidates[i].votes)
  134.  
  135.         {
  136.  
  137.             winning_candidate = candidates[i].name;
  138.  
  139.             printf("%s\n", winning_candidate);
  140.  
  141.         }
  142.  
  143.     }
  144.  
  145.     return;
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement