Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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];
  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. // Check for invalid usage
  39. if (argc < 2)
  40. {
  41. printf("Usage: runoff [candidate ...]\n");
  42. return 1;
  43. }
  44.  
  45. // Populate array of candidates
  46. candidate_count = argc - 1;
  47. if (candidate_count > MAX_CANDIDATES)
  48. {
  49. printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
  50. return 2;
  51. }
  52. for (int i = 0; i < candidate_count; i++)
  53. {
  54. candidates[i].name = argv[i + 1];
  55. candidates[i].votes = 0;
  56. candidates[i].eliminated = false;
  57. }
  58.  
  59. voter_count = get_int("Number of voters: ");
  60. if (voter_count > MAX_VOTERS)
  61. {
  62. printf("Maximum number of voters is %i\n", MAX_VOTERS);
  63. return 3;
  64. }
  65.  
  66. // Keep querying for votes
  67. for (int i = 0; i < voter_count; i++)
  68. {
  69.  
  70. // Query for each rank
  71. for (int j = 0; j < candidate_count; j++)
  72. {
  73. string name = get_string("Rank %i: ", j + 1);
  74.  
  75. // Record vote, unless it's invalid
  76. if (!vote(i, j, name))
  77. {
  78. printf("Invalid vote.\n");
  79. return 4;
  80. }
  81. }
  82.  
  83. printf("\n");
  84. }
  85.  
  86. // Keep holding runoffs until winner exists
  87. while (true)
  88. {
  89. // Calculate votes given remaining candidates
  90. tabulate();
  91.  
  92. // Check if election has been won
  93. bool won = print_winner();
  94. if (won)
  95. {
  96. break;
  97. }
  98.  
  99. // Eliminate last-place candidates
  100. int min = find_min();
  101. bool tie = is_tie(min);
  102.  
  103. // If tie, everyone wins
  104. if (tie)
  105. {
  106. for (int i = 0; i < candidate_count; i++)
  107. {
  108. if (!candidates[i].eliminated)
  109. {
  110. printf("%s\n", candidates[i].name);
  111. }
  112. }
  113. break;
  114. }
  115.  
  116. // Eliminate anyone with minimum number of votes
  117. eliminate(min);
  118.  
  119. // Reset vote counts back to zero
  120. for (int i = 0; i < candidate_count; i++)
  121. {
  122. candidates[i].votes = 0;
  123. }
  124. }
  125. return 0;
  126. }
  127.  
  128. // Record preference if vote is valid
  129. bool vote(int voter, int rank, string name)
  130. {
  131. for (rank=0; rank<candidate_count; rank++)
  132. {
  133. if (strcmp (name, candidates[rank].name) == 0)
  134. {
  135. strcpy (preferences[voter][rank], name);
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141.  
  142. // Tabulate votes for non-eliminated candidates
  143. void tabulate(void)
  144. {
  145. // TODO
  146. return;
  147. }
  148.  
  149. // Print the winner of the election, if there is one
  150. bool print_winner(void)
  151. {
  152. // TODO
  153. return false;
  154. }
  155.  
  156. // Return the minimum number of votes any remaining candidate has
  157. int find_min(void)
  158. {
  159. // TODO
  160. return 0;
  161. }
  162.  
  163. // Return true if the election is tied between all candidates, false otherwise
  164. bool is_tie(int min)
  165. {
  166. // TODO
  167. return false;
  168. }
  169.  
  170. // Eliminate the candidate (or candidiates) in last place
  171. void eliminate(int min)
  172. {
  173. // TODO
  174. return;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement