Advertisement
noob339

Untitled

Jan 12th, 2022
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.06 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // Max voters and candidates
  6. #define MAX_VOTERS 100
  7. #define MAX_CANDIDATES 9
  8.  
  9. // preferences[i][j] is jth preference for voter i
  10. int preferences[MAX_VOTERS][MAX_CANDIDATES]; //x or row is voter and y or column is the prference so 100 rows and 9 columns
  11.  
  12. // Candidates have name, vote count, eliminated status
  13. typedef struct
  14. {
  15.     string name;
  16.     int votes;
  17.     bool eliminated;
  18. }
  19. candidate;
  20.  
  21. // Array of candidates
  22. candidate candidates[MAX_CANDIDATES];
  23.  
  24. // Numbers of voters and candidates
  25. int voter_count;
  26. int candidate_count;
  27.  
  28. // Function prototypes
  29. bool vote(int voter, int rank, string name);
  30. void tabulate(void);
  31. bool print_winner(void);
  32. int find_min(void);
  33. bool is_tie(int min);
  34. void eliminate(int min);
  35.  
  36. int main(int argc, string argv[])
  37. {
  38.     //// line 38 - 43 is pretty self explanatory, checks that argc isn't less than 2, if so error code
  39.     // Check for invalid usage
  40.     if (argc < 2)
  41.     {
  42.         printf("Usage: runoff [candidate ...]\n");
  43.         return 1;
  44.     }
  45.     ////line 45 - 57 1. gets the count of candidates, which is argc - 1, 2. checks if more candidates than there should be and returns error code if there is, 3. use the name from the argv and assign to .name object and .votes, .elminated
  46.     // Populate array of candidates
  47.     candidate_count = argc - 1;
  48.     if (candidate_count > MAX_CANDIDATES)
  49.     {
  50.         printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
  51.         return 2;
  52.     }
  53.     for (int i = 0; i < candidate_count; i++)
  54.     {
  55.         candidates[i].name = argv[i + 1];
  56.         candidates[i].votes = 0;
  57.         candidates[i].eliminated = false;
  58.     }
  59.     ////line 59 - 64 checks the num of voters
  60.     voter_count = get_int("Number of voters: ");
  61.     if (voter_count > MAX_VOTERS)
  62.     {
  63.         printf("Maximum number of voters is %i\n", MAX_VOTERS);
  64.         return 3;
  65.     }
  66.  
  67.     // Keep querying for votes
  68.     for (int i = 0; i < voter_count; i++)
  69.     {
  70.  
  71.         // Query for each rank
  72.         for (int j = 0; j < candidate_count; j++)
  73.         {
  74.             string name = get_string("Rank %i: ", j + 1);
  75.  
  76.             // Record vote, unless it's invalid
  77.             if (!vote(i, j, name))
  78.             {
  79.                 printf("Invalid vote.\n");
  80.                 return 4;
  81.             }
  82.         }
  83.  
  84.         printf("\n");
  85.     }
  86.  
  87.     // Keep holding runoffs until winner exists
  88.     while (true)
  89.     {
  90.         // Calculate votes given remaining candidates
  91.         tabulate();
  92.  
  93.         // Check if election has been won
  94.         bool won = print_winner();
  95.         if (won)
  96.         {
  97.             break;
  98.         }
  99.  
  100.         // Eliminate last-place candidates
  101.         int min = find_min();
  102.         bool tie = is_tie(min);
  103.  
  104.         // If tie, everyone wins
  105.         if (tie)
  106.         {
  107.             for (int i = 0; i < candidate_count; i++)
  108.             {
  109.                 if (!candidates[i].eliminated)
  110.                 {
  111.                     printf("%s\n", candidates[i].name);
  112.                 }
  113.             }
  114.             break;
  115.         }
  116.  
  117.         // Eliminate anyone with minimum number of votes
  118.         eliminate(min);
  119.  
  120.         // Reset vote counts back to zero
  121.         for (int i = 0; i < candidate_count; i++)
  122.         {
  123.             candidates[i].votes = 0;
  124.         }
  125.     }
  126.     return 0;
  127. }
  128.  
  129. // Record preference if vote is valid
  130. bool vote(int voter, int rank, string name)
  131. {
  132.     bool flag = false;
  133.  
  134.     for (int i = 0; i < candidate_count; i++)
  135.     {
  136.         if (strcmp(candidates[i].name, name) == 0)
  137.         {
  138.             flag = true;
  139.             preferences[voter][rank] = i;
  140.         }
  141.     }
  142.  
  143.     return flag;
  144. }
  145.  
  146. // Tabulate votes for non-eliminated candidates
  147. void tabulate(void)
  148. {
  149.     // what should it do?
  150.     //How many things does it do?
  151.     //how does it do it?
  152.     // 1. update how many votes each candidate has in the first stage
  153.     //if eliminated the second stage
  154.  
  155.     for (int i = 0; i < voter_count; i++)
  156.     {
  157.         for (int j = 0; j < candidate_count; j++)
  158.         {
  159.             int n = preferences[i][j];
  160.             if (candidates[n].eliminated == false)
  161.             {
  162.                 candidates[n].votes++;
  163.                 break;
  164.             }
  165.         }
  166.     }
  167.  
  168.     return;
  169. }
  170.  
  171. // Print the winner of the election, if there is one
  172. bool print_winner(void)
  173. {
  174.     //consider the total number of votes and the number of votes in the first round. if the winner has 50 percent of the vote, win, else they loose
  175.  
  176.     bool result = false;
  177.  
  178.     for (int i = 0; i < candidate_count; i++)
  179.     {
  180.         if (candidates[i].votes > voter_count / 2)
  181.         {
  182.             printf("%s\n", candidates[i].name);
  183.             result = true;
  184.         }
  185.     }
  186.  
  187.     return result;
  188. }
  189.  
  190. // Return the minimum number of votes any remaining candidate has
  191. int find_min(void)
  192. {
  193.     // TODO
  194.  
  195.     int min = candidates[0].votes;
  196.  
  197.     for (int i = 0; i < candidate_count; i++)
  198.     {
  199.         if (candidates[i].votes < min && candidates[i].eliminated == false)
  200.         {
  201.             min = candidates[i].votes;
  202.         }
  203.     }
  204.  
  205.  
  206.     return min;
  207. }
  208.  
  209. // Return true if the election is tied between all candidates, false otherwise
  210. bool is_tie(int min)
  211. {
  212.  
  213.     bool isTie = false;
  214.  
  215.     int count = 0;
  216.  
  217.     int i = 0, j = 1;
  218.  
  219.  
  220.     for (i = 0; i < candidate_count; i++)
  221.     {
  222.         for (j = 1; j < candidate_count - i; j++)
  223.         {
  224.             if (candidates[i].votes == candidates[j + i].votes && candidates[i].votes == min)
  225.             {
  226.                 count++;
  227.                 isTie = true;
  228.             }
  229.         }
  230.     }
  231.  
  232.     //printf("%i", candidates[i].votes);
  233.     //printf("%i", candidates[j+i].votes);
  234.     //printf("\n");
  235.  
  236.     //printf("%i\n", count);
  237.  
  238.     return isTie;
  239. }
  240.  
  241. // Eliminate the candidate (or candidates) in last place
  242. void eliminate(int min)
  243. {
  244.     for (int i = 0; i < candidate_count; i++)
  245.     {
  246.         if (candidates[i].votes == min && candidates[i].eliminated == false)
  247.         {
  248.             candidates[i].eliminated = true;
  249.         }
  250.     }
  251.     return;
  252. }
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement