user12027492
user12027492

Reputation:

Plurality - how to print up multiple winner if number of votes are equal?

Plurality program - The program takes input from user and prints the candidate with the most number of votes. I got two problems and I want that print_winner prints multiple winners in case of tie and it prints all the winners when everyone is tied? How do you suggest I do that?

If there are multiple winners with the same number of votes, how do you suggest I do that? While answering keep the words simple as I am new to programming and don't really get the difficult terms.

P.S I did the assignment, this is just a additional feature I want to add to it.

Thanks

#include <cs50.h>

#include <stdio.h>

#include <string.h>

#include <stdbool.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
    bool exist = false;

    for (int i = 0; i < candidate_count; i++) 
    {
        //check if the typed in name is in the list of candidates
        if (strcmp(name, candidates[i].name) == 0) 
        {
            candidates[i].votes += 1;
            exist = true;
            break;
        }

    }

    return exist;
}

// Print the winner (or winners) of the election
void print_winner(void) 
{
    int most = candidates[0].votes;
    string winner = candidates[0].name;
    for (int i = 1; i < candidate_count; i++) 
    {
        if (most < candidates[i].votes) 
        {
            most = candidates[i].votes;
            winner = candidates[i].name;
        }
    }

    //printf("%d", most);
    printf("%s\n", winner);
    return;
}

/* Print the winner (or winners) of the election
void print_winner(void) {
  int most = candidates[0].votes;
  string winner = candidates[0].name;
  for (int i = 1; i < candidate_count; i++) {
    if (most < candidates[i].votes) {
      most = candidates[i].votes;
    }

  }
  for (int j = 0; j < candidate_count; j++) {
    if (candidates[j].votes == most) {
      winner = candidates[j].name;
      printf("%s", winner);
    }
  }
}*/

Upvotes: 1

Views: 5185

Answers (3)

Yemi Bold
Yemi Bold

Reputation: 366

your code does what you are trying todo but if you want make it more better try to cmpare if there are more candidates that have the same score as the most then have an array of winners and then print them all.

Upvotes: 0

DinoCoderSaurus
DinoCoderSaurus

Reputation: 6520

According to the spec program must handle "multiple winners", it's not an "additional feature":

It is possible that the election could end in a tie if multiple candidates each have the maximum number of votes. In that case, you should output the names of each of the winning candidates, each on a separate line.

Think about what you need to know to print_winner. You need to know what is the "most" votes, as program currently does in a loop. Additionally, you need to know who got that many votes. That would require another loop through the "candidates", printing each one that has the "most".

Upvotes: 1

ikegami
ikegami

Reputation: 386406

To print multiple winners, you'll need a loop. Specifically, you could loop all over the candidates, and print out those that have a number of votes equal to most.

Upvotes: 0

Related Questions