CS50 Plurality Solution

By Suraj Chaudhary

Are you looking for the solution of Harvard’s CS50 PSet 3? Do you want the CS50 Plurality Solution of week 3?

If so you’re in the right place.

In this article, I’m going to share with you exactly how to solve the CS50 Plurality problem and will also share the CS50 Plurality solution with you.

Here’s how to solve the CS50 Plurality problem.

The first thing to do is to understand what we are trying to build. You can read the instructions from CS50’s website.

In the plurality vote, every voter gets to vote for one candidate. At the end of the election, whichever candidate has the greatest number of votes is declared the winner of the election.

For this problem, we will implement a program that runs a plurality election, per the explanation in the problem.

Here’s my entire code for the CS50 Plurality Solution:

#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)
{
    // TODO
    int maxvotes = 0;

    // find max votes
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > maxvotes)
        {
            maxvotes = candidates[i].votes;
        }
    }

    // print candidates with highest votes
    for (int j = 0; j < candidate_count; j++)
    {
        if (candidates[j].votes == maxvotes)
        {
            printf("%s\n", candidates[j].name);
        }
    }
    return;
}

If you have any confusion or problem, feel free to drop a comment down below. In the meantime, you can read these articles.

Also Read

  1. CS50: How To Study At Harvard For FREE (and get a free certificate)
  2. How to use Windows 11 on Mac for Free
  3. How To Get A Domain Name For FREE
  4. How To Use Microsoft Office Apps For FREE Legally (Mac & PC)

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resources for living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

Leave a Comment

Slide to prove you're not a bot/spammer *