Advertisement
Mannawar

Untitled

Jan 11th, 2020
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 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. //1. Record preference if vote is valid
  129. bool vote(int voter, int rank, string name)
  130. {
  131.     // TODO
  132.  
  133.     for (int i = 0; i <= candidate_count; i++)//to iterate through every candidates (columns)
  134.     {
  135.         int j = 0;
  136.         if (strcmp(candidates[j].name, name) == 0)
  137.         {
  138.             preferences[voter][rank] = candidate_count;
  139.             return true;
  140.         }
  141.     }
  142.     return false;
  143. }
  144.  
  145. //2. Tabulate votes for non-eliminated candidates
  146. void tabulate(void)
  147. {
  148.     // TODO
  149.     return;
  150. }
  151.  
  152. //3. Print the winner of the election, if there is one
  153. bool print_winner(void)
  154. {
  155.     // TODO
  156.     return false;
  157. }
  158.  
  159. //4. Return the minimum number of votes any remaining candidate has
  160. int find_min(void)
  161. {
  162.     // TODO
  163.     return 0;
  164. }
  165.  
  166. //5. Return true if the election is tied between all candidates, false otherwise
  167. bool is_tie(int min)
  168. {
  169.     // TODO
  170.     return false;
  171. }
  172.  
  173. //6. Eliminate the candidate (or candidiates) in last place
  174. void eliminate(int min)
  175. {
  176.     // TODO
  177.     return;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement