Plurality - CS50x
Plurality - CS50x
OpenCourseWare
Plurality
Implement a program that runs a plurality election, per the below.
Background
Elections come in all shapes and sizes. In the UK, the Prime Minister (https://www.parliament.uk/education/about-your-parliament/general-
elections/) is of cially appointed by the monarch, who generally chooses the leader of the political party that wins the most seats in the House
of Commons. The United States uses a multi-step Electoral College (https://www.archives.gov/federal-register/electoral-college/about.html)
process where citizens vote on how each state should allocate Electors who then elect the President.
Perhaps the simplest way to hold an election, though, is via a method commonly known as the “plurality vote” (also known as “ rst-past-the-
post” or “winner take all”). 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.
Getting Started
Here’s how to download this problem’s “distribution code” (i.e., starter code) into your own CS50 IDE. Log into CS50 IDE (https://ide.cs50.io/)
and then, in a terminal window, execute each of the below.
Understanding
Let’s now take a look at plurality.c and read through the distribution code that’s been provided to you.
The line #define MAX 9 is some syntax used here to mean that MAX is a constant (equal to 9 ) that can be used throughout the program.
Here, it represents the maximum number of candidates an election can have.
The le then de nes a struct called a candidate . Each candidate has two elds: a string called name representing the candidate’s
name, and an int called votes representing the number of votes the candidate has. Next, the le de nes a global array of candidates ,
where each element is itself a candidate .
Now, take a look at the main function itself. See if you can nd where the program sets a global variable candidate count representing the
1/4
Now, take a look at the main function itself. See if you can nd where the program sets a global variable candidate_count representing the
number of candidates in the election, copies command-line arguments into the array candidates , and asks the user to type in the number of
voters. Then, the program lets every voter type in a vote (see how?), calling the vote function on each candidate voted for. Finally, main
makes a call to the print_winner function to print out the winner (or winners) of the election.
If you look further down in the le, though, you’ll notice that the vote and print_winner functions have been left blank. This part is up to
you to complete!
Speci cation
Complete the implementation of plurality.c in such a way that the program simulates a plurality vote election.
You should not modify anything else in plurality.c other than the implementations of the vote and print_winner functions (and the
inclusion of additional header les, if you’d like).
Usage
Walkthrough
2/4
Testing
Execute the below to evaluate the correctness of your code using check50 . But be sure to compile and test it yourself as well!
check50 cs50/problems/2020/x/plurality
Execute the below to evaluate the style of your code using style50 .
style50 plurality.c
How to Submit
Execute the below, logging in with your GitHub username and password when prompted. For security, you’ll see asterisks ( * ) instead of the
actual characters in your password.
submit50 cs50/problems/2020/x/plurality
3/4
4/4