SHARE
TWEET

runoff_test1

a guest Jan 24th, 2020 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. void tabulate(void);
  6.  
  7. typedef struct
  8. {
  9.     string name;
  10.     int votes;
  11.     bool eliminated;
  12. }
  13. candidate;
  14.  
  15. candidate candidates[4];
  16. int candidate_count = 4;
  17.  
  18. int preferences[11][4];
  19. string name[11][4];
  20. int voter_count = 11;
  21.  
  22. int main(void)
  23. {
  24.     name[0][0] = "Tom";
  25.     name[0][1] = "Dick";
  26.     name[0][2] = "Harry";
  27.     name[0][3] = "Justin";
  28.    
  29.     name[1][0] = "Tom";
  30.     name[1][1] = "Dick";
  31.     name[1][2] = "Harry";
  32.     name[1][3] = "Justin";
  33.    
  34.     name[2][0] = "Tom";
  35.     name[2][1] = "Dick";
  36.     name[2][2] = "Harry";
  37.     name[2][3] = "Justin";
  38.    
  39.     name[3][0] = "Dick";
  40.     name[3][1] = "Tom";
  41.     name[3][2] = "Harry";
  42.     name[3][3] = "Justin";
  43.    
  44.     name[4][0] = "Dick";
  45.     name[4][1] = "Tom";
  46.     name[4][2] = "Harry";
  47.     name[4][3] = "Justin";
  48.    
  49.     name[5][0] = "Dick";
  50.     name[5][1] = "Tom";
  51.     name[5][2] = "Harry";
  52.     name[5][3] = "Justin";
  53.    
  54.     name[6][0] = "Harry";
  55.     name[6][1] = "Tom";
  56.     name[6][2] = "Dick";
  57.     name[6][3] = "Justin";
  58.    
  59.     name[7][0] = "Harry";
  60.     name[7][1] = "Tom";
  61.     name[7][2] = "Dick";
  62.     name[7][3] = "Justin";
  63.    
  64.     name[8][0] = "Harry";
  65.     name[8][1] = "Tom";
  66.     name[8][2] = "Dick";
  67.     name[8][3] = "Justin";
  68.    
  69.     name[9][0] = "Justin";
  70.     name[9][1] = "Dick";
  71.     name[9][2] = "Tom";
  72.     name[9][3] = "Harry";
  73.    
  74.     name[10][0] = "Justin";
  75.     name[10][1] = "Dick";
  76.     name[10][2] = "Tom";
  77.     name[10][3] = "Harry";
  78.    
  79.     candidates[0].name = "Tom";
  80.     candidates[0].votes = 0;
  81.     candidates[0].eliminated = false;
  82.    
  83.     candidates[1].name = "Dick";
  84.     candidates[1].votes = 0;
  85.     candidates[1].eliminated = false;
  86.    
  87.     candidates[2].name = "Harry";
  88.     candidates[2].votes = 0;
  89.     candidates[2].eliminated = false;
  90.    
  91.     candidates[3].name = "Justin";
  92.     candidates[3].votes = 0;
  93.     candidates[3].eliminated = true;
  94.    
  95.     tabulate();
  96.    
  97.     for (int i = 0; i < candidate_count - 1; i++)
  98.     {
  99.         if (candidates[i].eliminated)
  100.         {
  101.             printf("%s ELIMINATED\n", candidates[i].name);
  102.         }
  103.         else if (!candidates[i].eliminated)
  104.         {
  105.             printf("%s total: %i\n", candidates[i].name, candidates[i].votes);
  106.         }
  107.     }
  108.        
  109. }
  110.  
  111. // Tabulate votes for non-eliminated candidates
  112. void tabulate(void)
  113. {
  114.     // For each voter, if the voter's highest choice has been eliminated, move on the the next choice. Otherwise, spend the voter's one vote on their highest choice still in the running.
  115.     int choice = 0;
  116.    
  117.     for (int i = 0; i < voter_count - 1; i++)
  118.     {
  119.         int vote = 1;
  120.        
  121.         for (int j = 0; j < candidate_count - 1; j++)
  122.         {
  123.             if (strcmp(candidates[j].name, name[i][choice]) == 0 && candidates[j].eliminated)
  124.             {
  125.                 choice++;
  126.             }
  127.             else if (strcmp(candidates[j].name, name[i][choice]) == 0 && !candidates[j].eliminated)
  128.             {
  129.                 candidates[j].votes = candidates[j].votes + vote;
  130.                 vote = 0;
  131.             }
  132.         }
  133.     }
  134.    
  135.     return;
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top