BRIAN YU: Let's take a look at plurality.
In plurality, your task is going to be to write
a program that simulates an election.
Your program will take a sequence of votes as input,
and your program will output the winner of that election.
Let's take a look at how you might do that.
Your election is going to have a sequence of candidates in the election.
This election has Alice, Bob, and Charlie as the candidates, for example.
And each voter is going to select one of those candidates to vote for.
If you take this voter's information in combination
with the information from all of the other voters,
you can use that information to figure out who the winner of the election is.
What information will you need to keep track of?
Well, you'll need to keep track of each of the candidates,
and for each of the candidates, how many votes they have.
In this case, Alice has three votes, Bob has one vote,
and Charlie has two votes.
So as a result, Alice is declared the winner of the election.
This is what we call a plurality vote, also known as first past the post.
The rules basically work like this.
Every voter chooses one candidate who they would like to vote for.
And at the end of the election, whichever candidate has
the most votes is declared the winner of that election.
So how is your program going to work?
Well, your program is going to be run as ./plurality followed by the names
of all of the candidates in the election.
In this case, Alice, Bob, and Charlie.
Your program will then prompt the user for the number
of voters in the election.
In this case, we might say four, for example.
And then now four times your program is going
to prompt the voter to type in who they're going to vote for.
So the first vote for Alice, for example.
And then the next voter might vote for Bob, for example.
And maybe the last two voters are going to vote for Charlie.
After all of the votes are in, your program
is going to figure out who the winner of the election is and output their name.
In this case, Charlie.
So how are you going to do this?
Well, recall that this is the data that you need to store.
For each candidate, you need to keep track of how many votes
they currently have, which means that for any candidate,
you're really storing two pieces of information.
And when you're storing multiple pieces of information,
one convenient way to represent that in a c program
is by storing the information inside of a struct.
Here, we've defined a struct called a candidate, inside
of which we're going to store two fields.
One field which is a string called name representing the candidate's name,
and one field which is an integer called votes representing the number of votes
that this candidate currently has.
We can take all of these candidates stored inside of these structs
and put them in a sequence by creating an array of candidates which we're
going to call candidates that's going to store all of the candidates that
are currently in the election.
Because this is an array, we can index into this array the same way we index
into any other array by using candidates[0] to mean the first
candidate in the election, candidates[1] to mean the second candidate,
and candidates[2] to mean the third candidate.
But remember that each candidate is itself
a struct that has a name and a number of votes.
So if we want to take this struct and access a property of it,
we could say candidates[2].name, for example--
using the dot syntax to access the name of this candidate--
and using candidates[2].votes to access the number of votes that Charlie
currently has.
So what are you going to do in plurality?
Well, you're going to implement two functions.
One called vote and one called a print_winner.
Your vote function is going to take as its argument
a string parameter called name.
And what your vote function is going to do
is it's going to look for a candidate who's
called name, who has that same name.
And if you're able to find a candidate with that name,
you're going to update their vote total and return true.
Otherwise, if you aren't able to find that candidate,
you're going to not update any vote totals at all,
and you're instead going to return false.
So how might that work?
Well, if the vote function is called on Bob,
for example, you might want to iterate through this array of candidates
starting with the first one and asking yourself,
does this candidate's name match Bob?
In this case, it doesn't.
So we move on.
We'd move to the next candidate.
Does this candidate's name match Bob?
Well, it does.
So we found a match, which means we can take the vote total and increment it,
moving it from 1 to 2 to keep track of this new vote.
And then our vote function would return true.
Of course, if we had voted for someone who wasn't in this election at all--
for example, Dave--
we wouldn't update any vote totals, and our function would return false.
So that's the vote function.
The second function you're going to implement in this program
is print_winner.
And the print_winner function is going to be tasked with printing out
the winner of the election.
So in this function, you're going to print out
the name of the candidate or candidates who have the most votes.
Because remember, it's possible that at the end of the election,
multiple people are tied for the most number of votes, in which case
each of them should be declared a winner.
And you'll want to print out each of their names.
So in this case where Alice has three votes, Bob has one vote,
and Charlie has two votes, Alice has the most votes,
and Alice is going to be declared the winner of the election.
So you should print out Alice's name.
But consider, for example, these vote totals
where both Alice and Bob each are tied for winning
the election at three votes each.
So your program should print out Alice on one line and then Bob
on the next line.
After you've implemented these two functions, vote and print_winner,
you should have implemented all you need to be
able to simulate a plurality election.
And by running ./plurality, you should be able to type in a sequence of votes
and have your program output who the winner of the election is.
My name is Brian.
And this was plurality.