1

This program is for counting the plurality for a list of candidates, Check Description for more info, The program works like this:

  1. Enter the candidate's name as an argument when running the program for example:
./plurality John Jack Tom Timara
  1. The program will ask you to enter the Number of voters
  2. The program will ask you for your vote, It should be one of the candidates' names (Case Sensitive).
  3. After you enter the candidate's name the program will ask you again for your vote depend on your Number of voters.
  4. The program will output the winning candidate. (Could be more than one candidate if they are equal)

What is my problem?

Let's say when I enter Number of voters: 6 and vote 3 times for John and 3 times for Jack the output will be:

The winner is: John, 

But if I vote 3 times for John and 3 times for Tom or Timara the output will be:

The winner is: John, Tom

The program doesn't work correctly if the first candidate and the second have an equal amount of votes and I don't know why! Here is My code:

#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)
{
    bool exist = false;

    for (int i = 0; i < candidate_count; i++)
    {
        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)
{
    candidate candidateHolder;
    string heighest = "";

    printf("The winner is: ");

    for (int i = 0; i < candidate_count; i++)
    {
       if (i == 0)
        {
            candidateHolder = candidates[i];
            heighest = candidateHolder.name;
            continue;
        }
        else if (candidateHolder.votes < candidates[i].votes)
        {
            candidateHolder = candidates[i];
            heighest = candidateHolder.name;
        }
        else if (candidateHolder.votes == candidates[i].votes)
        {
            candidateHolder = candidates[i];
            strcat(heighest, ", ");
            strcat(heighest, candidateHolder.name);
        }
    }
    printf("%s\n", heighest);
}
  • I'm having a lot of problems doing plurality too. Can you explain the bool exist part? – Betty Van Oct 24 '19 at 20:21
  • First, when the function vote runs I'll assume that the candidate that I'm looking for does not exist so I create a new bool variable called exist and set its default value to false. Next, I create a loop to look at each candidate in candidates[i].name, to do that you need to use a function called strcmp(), you can learn more about it from here. The strcmp() function compares two strings if they are equal the return value will be 0, (continue in the next comment). – Budi Salah Oct 25 '19 at 16:30
  • So in my condition if (strcmp(name, candidates[i].name) == 0) if its true I'll update the candidate[i].votes and add 1 to it, then I'll update the exist bool to equal true and then I'll out from this loop by using break so I don't waste more time searching for the candidate. then at the end of the function I'll return the current exist value. – Budi Salah Oct 25 '19 at 16:30
  • I appreciate the help. I dont think there was a lot of documentation on this problem set. This is the best I could find. The lecture doesn't quite explain this, they get into the basics of typedef struct. For example intializing a new arrary candidate candidateholder; is greek to me. I would like to see another solution to the same problem highly commented or runoff or tide, subsequent problems in the series, but I super appreciate this post alot. – msippel415 Dec 17 '19 at 14:13
4

You should rethink your logic for the print_winners function.

Here's the pseudocode I'd use.

  • Declare a maxvote int set to 0.
  • Iterate over the list of candidates and check the number of votes. If that number is greater than the maxvote, update the maxvote to that number. Once the loop finishes, you now have maxvote.

  • Next, again iterate over the list of candidates and print (as you go) each candidate who has that maxvote number of votes.

There's no need as you've done to create a new string and then try to manipulate that.

As the spec shows, the winner(s) should simply be printed at the end, one per line.

$ ./plurality Alice Bob Charlie
Number of voters: 5
Vote: Alice
Vote: Charlie
Vote: Bob
Vote: Bob
Vote: Alice
Alice
Bob
  • thank you for your answer, Quick question, I always have trouble in logic and writing pseudocode, So how I can improve them? – Budi Salah Oct 25 '19 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .