Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 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. //Record vote for each voter based on preference
  132. for (int i = 0; i < candidate_count; i++)
  133. {
  134.  
  135.  
  136. if (strcmp(candidates[i].name, name) == 0)
  137. {
  138. preferences[voter][rank] = i;
  139. return true;
  140. }
  141.  
  142. }
  143. // TODO
  144. return false;
  145. }
  146.  
  147. // Tabulate votes for non-eliminated candidates
  148. void tabulate(void)
  149. {
  150. for (int i = 0; i < voter_count; i++)
  151. {
  152. int j = 0;
  153.  
  154. if( candidates[preferences[i][j]].eliminated == true)
  155. {
  156. j++;
  157. }
  158.  
  159. else
  160. {
  161. candidates[preferences[i][j]].votes++;
  162. break;
  163. }
  164.  
  165. }
  166. // TODO
  167. return;
  168. }
  169.  
  170. // Print the winner of the election, if there is one
  171. bool print_winner(void)
  172. {
  173. int majority = voter_count / 2 + .5;
  174.  
  175. for(int i = 0; i < candidate_count; i++)
  176. {
  177.  
  178. if (candidates[i].votes > majority)
  179. {
  180. return true;
  181. printf("%s\n", candidates[i].name);
  182.  
  183. bool winner = true;
  184. printf("%s\n", candidates[i].name);
  185. return winner;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }return false;
  192.  
  193. }
  194.  
  195. // Return the minimum number of votes any remaining candidate has
  196. int find_min(void)
  197. {
  198. int smallest_vote = candidates[0].votes;
  199.  
  200. for (int k = 0; k < candidate_count; k++)
  201. {
  202. if (candidates[k].eliminated == false && smallest_vote > candidates[k].votes)
  203. {
  204. smallest_vote = candidates[k].votes;
  205. }
  206.  
  207. } return smallest_vote;
  208. // TODO
  209. }
  210.  
  211. // Return true if the election is tied between all candidates, false otherwise
  212. bool is_tie(int min)
  213. {
  214. for (int i = 0; i < candidate_count; i++)
  215. {
  216. int not_eliminated = 0;
  217. int counter = 0;
  218.  
  219. if (candidates[i].eliminated != 0)
  220. {
  221. not_eliminated ++;
  222. }
  223. if (candidates[i].votes == min)
  224. {
  225. counter++;
  226. }
  227. if (not_eliminated == counter)
  228. {
  229. return true;
  230. }
  231. if (not_eliminated != counter)
  232. {
  233. return false;
  234. }
  235. }
  236. // TODO
  237. return false;
  238. }
  239.  
  240. // Eliminate the candidate (or candidiates) in last place
  241. void eliminate(int min)
  242. {
  243. for (int i = 0; i < candidate_count; i++)
  244. {
  245. if (candidates[i].eliminated == false && candidates[i].votes == min)
  246. {
  247. candidates[i].eliminated = true;
  248. }
  249.  
  250. }
  251. // TODO
  252. return;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement