i will send some code, i want to arrange the candidates into a list according to the…
- User
i will send some code, i want to arrange the candidates into a list according to the number of votes they got, highest on top #include <cs50.h> #include <stdio.h> #include <string.h>
// Max number of candidates #define MAX 9
// Candidates have name and vote count typedef struct { string name; int votes; } candidate;
// Array of candidates candidate candidates[MAX];
// Number of candidates int candidate_count;
// Function prototypes bool vote(string name); void print_winner(void);
int main(int argc, string argv[]) { // Check for invalid usage if (argc < 2) { printf("Usage: plurality [candidate ...]\n"); return 1; }
// Populate array of candidates candidate_count = argc - 1; if (candidate_count > MAX) { printf("Maximum number of candidates is %i\n", MAX); return 2; } for (int i = 0; i < candidate_count; i++) { candidates[i].name = argv[i + 1]; candidates[i].votes = 0; } int voter_count = get_int("Number of voters: "); // Loop over all voters for (int i = 0; i < voter_count; i++) { string name = get_string("Vote: "); // Check for invalid vote if (!vote(name)) { printf("Invalid vote.\n"); } } // Display winner of election print_winner();}
// Update vote totals given a new vote bool vote(string name) { // TODO for(int i = 0; i < candidate_count; i++) { if(strcmp(candidates[i].name, name) == 0) { candidates[i].votes++; return true; }
} return false;}
// Print the winner (or winners) of the election void print_winner(void) { string name[] = {}; int maxvotes = 0; int length = 0; // TODO //sort the candidate list according to the number of votes for(int i = 0; i < candidate_count - 1; i++) {
} printf("%i\n", length); for(int i = 0; i < length; i++) { printf("%s\n", name[i]); } return;}
- Assistant
To arrange the candidates into a list according to the number of votes they got, highest on top, you can modify the
print_winnerfunction. Here's the updated code:// Print the winner (or winners) of the election void print_winner(void) { int max_votes = 0; // Find the maximum number of votes for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes > max_votes) { max_votes = candidates[i].votes; } } // Print the candidates with the maximum number of votes for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes == max_votes) { printf("%s\n", candidates[i].name); } } return; }In this modified
print_winnerfunction, we first find the maximum number of votes by iterating over all the candidates. Then, we iterate over the candidates again and print the names of those candidates whose votes equal the maximum number of votes.Note that this code does not sort the candidates by the number of votes, as sorting is not required based on your question. If you need the candidates to be sorted, please let me know.
- User
how to modify in case two candidates[i].votes have the same value and they are both max_votes, so I want to show them both
- Assistant
To modify the code so that it prints all the candidates who have the same maximum number of votes, you can store the indices of these candidates in an array and then iterate over that array to print the names. Here's the updated code:
// Print the winner (or winners) of the election void print_winner(void) { int max_votes = 0; int winner_indices[MAX]; int num_winners = 0; // Find the maximum number of votes for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes > max_votes) { max_votes = candidates[i].votes; } } // Store the indices of the candidates with the maximum number of votes for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes == max_votes) { winner_indices[num_winners] = i; num_winners++; } } // Print the names of the winners for (int i = 0; i < num_winners; i++) { printf("%s\n", candidates[winner_indices[i]].name); } return; }