FatherPhi video
CS50 Plurality Solution - PSET 3 | 2021 Step by Step Tutorial | Live Coding
12.7K views ยท 230 likes ยท 44 comments
About this video
Vote for the next video here
๐๐๐
https://FatherPhi.com
Eleven labs is the crazy ai voice generation tool I use to fool ChatGPT
Try it out for free!๐ https://try.elevenlabs.io/6wbvspyzcq44
(Affiliate link)
For the weekly brief on accessible and practical Ai usage + updates on our latest mission join the newsletter
๐ https://master-of-ai.com
๐ฉ For business inquiries please reach out to phi@fatherphi.com
And since people are asking.
The camera I use to shoot these videos
๐น DJI Osmos Pocket 3 ๐ https://amzn.to/3O3Qw8R
Transcript
what's up guys it's codepony here and
today we're going to be doing pset 3
plurality and we're going to be doing it
in the easiest way
humanly possible and let's get right to
it
all right as you can see
i passed all of the conditions for check
50
and we're going to start from scratch
so i like to break down my problems into
main problems and then sub problems
before i do any programming
so the two main problems would be vote
counting and then
finding the number highest number of
votes so if we break down
the vote counting into its own
subproblems
we have with the count votes of each
candidate and for each count
and for each vote count and in number of
times in being the number of candidates
so let's say we have three candidates
we'll have to count their votes
three times so i like to start with
pseudocode and
i like to break down my problems into
two main problems
and then sub problems after afterwards
so that we can really get a grasp of the
situation
and then we don't just start coding
randomly
so the two main problems would be vote
counting and then finding the highest
number of votes
so as for the vote sub problems we have
to count votes for each candidate
and we have to count votes for an n
number of times n being the number of
votes that the user gives us
so when we see an in number of times for
a problem i would normally suggest a
loop
we must also check the candidate name
given by the user to see if it's a match
for any of our other candidates and this
can be pretty easily done
with structs and which we already have
provided
and then we have the print winner
function where we must check our
candidate vote numbers
and then number of times so in here
being the number of candidates that we
have
and as we check the vote tallies we have
to save the top candidate each time to
find a candidate with a higher vote
we also have to make sure to print all
the winners even in cases of a tie
so let's get started
here we have just some declarations of
variables and
struct it really sets the stage for us
to work
with here we have a struct called
candidate that we can work with and then
they
already pre-make us a list of these
candidates with
using the max number nine so it's going
to be a max number of nine candidates in
the list
and then this is the number of
candidates
that the user is providing us that's the
candidate count
and we get that by asking the user how
many candidates
we get the candidate account by taking
the number of candidates in the string
that they provide us
and then we just minus one we don't
count the
the slash plurality argument
and this is just checking the num that
we have at least
um one candidate
then here we're gonna start going in and
setting
each candidate in the list so let's say
we have joe and mary
it'll be joe said said his name to joe
set his votes to zero
and then go to mary set her name to mary
and then set her votes
theorem
we're going to loop over all the voters
so
each time someone each time the user
votes
they're going to type in joe and then
it's going to use the vote function
and if it's an invalid vote it'll say so
if not it'll add one
to his vote count and then it's going to
move on to the next name
on the list like the next input so if
the next input is mary
then it's going to do the same thing
it's going to check to see if it mary
matches the candidate list
and then it's going to increment mary's
vote by one
and then we're just going to print the
winner so first things first
we're going to work on the vote function
so the vote function is going to be
pretty simple it's going to be
mostly a big for loop
and we're going to take an int and
to keep track of the for loop and i'm
going to name it current candidate
and i'm going to copy that because it's
a pretty long
[Music]
variable name but it's just to make it
really simple for you
so we're going to keep it at zero we're
going to declare it at zero first
to start at the beginning of the list
and then as long as current
candidate i can just paste
is less than candidate
count
and then we're going to iterate for each
for each time we go through we're gonna
increment the current candidate each
time we go through the loop
so see like a lot of other programming
languages has
um functions already built into the
library and one of them is string
compare
and what we're going to do here is each
time each time we go through this
loop we're going to use string compare
and compare
the current candidate in the list that
we're looking at to the
string name here that we're given
through the vote function so up here the
user is going to
give us who they're voting for right
here
and then they're going to feed it to us
here
so name we're going to use it here
so if string compare
name is equal
is we're going to feed string compare
two
arguments it's going to be name and
candidates
current candidate dot name
and if that returns a 0 then
we know that the two
are the same that the name that the user
gave us
let's say joe matched the candidate on
the list
one of the candidates on the list so if
it's true
then we just
then we just increment candidates
by one
and then we have to return true
we return true because that way this
won't go into the invalid vote
uh if statement so that's it for the
vote
function now we're going to move on to
the print winner function
so here we're going to have to keep
track of candidate with the highest
number of votes
so to do that we're going to declare
variable
top candidate
number of votes so
we we need to keep track of the number
of votes not the candidate name because
there can be a tie
so let's say we have two people with two
votes each
then we have to be able to find both of
them
in the candidates list
so while current candidate
is less than candidate count
oh
and then we have to remember to
increment current candidate
and i like to do it before i do any of
the logic inside just i don't forget
it's a really easy thing to forget so
in within this while loop what we're
going to be doing is we're going to be
setting
the top candidate number of votes so
all we have to do is check if
top candidate number
of votes and that's actually pretty long
so i'm going to copy that one for later
use
it's less than um
candidates current
candidate
dot votes
then what we're going to do is we're
going to set it to that
we're going to set top candidate number
of votes
to candidate
to the current candidate that we're
looking at
okay then now now we we now we have this
while loop here it's going to give us
the the top
so now that we have this while loop here
it's going to give us the top candidate
number of votes
so once now that we have that number we
have to loop through the list of
candidates
and print out every time we hit that
number
we find a candidate with that number of
votes
so we're gonna use the for loop here and
since i already declared current
candidate up here
i can just
reset it right here
so as long as current candidate
is less than a candidate count
and then iterate
i know increment
i keep saying it right
so here all you have to do is check if
candidates
current candidate
votes is equal to
the top candidate number of votes
it's going to print f
string new line
candidates it's going to take from the
candidates list
at the on the current candidate
candidate that we're looking at
and we're going to take the name and
just print and plug it into the printf
that should do it let's see if i have to
have any
typos
all right so i'm going to do check 50
here to make sure
everything's good
oh and as i pressed check 50 i noticed
that that slash n was
a forward slash it needs to be a
backslash in so
i'm gonna it's gonna fail here and i'm
gonna try it again
make plurality check 50.
see like those little things can throw
it all off
all right there we go it passed and i'm
going to be providing this code in
my code phony github and if you have a
better solution you know feel free to
leave it down in the comments it helps
anyone who watches this video later on
because a lot of people check the
comments and they see a better solution
or they see something that clicks better
with them you know
and my main priority is just to make
sure that you understand what's going on
and how these basic programming concepts
come together and we
can really use all those basic tools
later on
and that's what's really important so if
you like this video hit give me a like
and subscribe maybe and i would really
appreciate it
and see you next time because it's
codepony out peace
today we're going to be doing pset 3
plurality and we're going to be doing it
in the easiest way
humanly possible and let's get right to
it
all right as you can see
i passed all of the conditions for check
50
and we're going to start from scratch
so i like to break down my problems into
main problems and then sub problems
before i do any programming
so the two main problems would be vote
counting and then
finding the number highest number of
votes so if we break down
the vote counting into its own
subproblems
we have with the count votes of each
candidate and for each count
and for each vote count and in number of
times in being the number of candidates
so let's say we have three candidates
we'll have to count their votes
three times so i like to start with
pseudocode and
i like to break down my problems into
two main problems
and then sub problems after afterwards
so that we can really get a grasp of the
situation
and then we don't just start coding
randomly
so the two main problems would be vote
counting and then finding the highest
number of votes
so as for the vote sub problems we have
to count votes for each candidate
and we have to count votes for an n
number of times n being the number of
votes that the user gives us
so when we see an in number of times for
a problem i would normally suggest a
loop
we must also check the candidate name
given by the user to see if it's a match
for any of our other candidates and this
can be pretty easily done
with structs and which we already have
provided
and then we have the print winner
function where we must check our
candidate vote numbers
and then number of times so in here
being the number of candidates that we
have
and as we check the vote tallies we have
to save the top candidate each time to
find a candidate with a higher vote
we also have to make sure to print all
the winners even in cases of a tie
so let's get started
here we have just some declarations of
variables and
struct it really sets the stage for us
to work
with here we have a struct called
candidate that we can work with and then
they
already pre-make us a list of these
candidates with
using the max number nine so it's going
to be a max number of nine candidates in
the list
and then this is the number of
candidates
that the user is providing us that's the
candidate count
and we get that by asking the user how
many candidates
we get the candidate account by taking
the number of candidates in the string
that they provide us
and then we just minus one we don't
count the
the slash plurality argument
and this is just checking the num that
we have at least
um one candidate
then here we're gonna start going in and
setting
each candidate in the list so let's say
we have joe and mary
it'll be joe said said his name to joe
set his votes to zero
and then go to mary set her name to mary
and then set her votes
theorem
we're going to loop over all the voters
so
each time someone each time the user
votes
they're going to type in joe and then
it's going to use the vote function
and if it's an invalid vote it'll say so
if not it'll add one
to his vote count and then it's going to
move on to the next name
on the list like the next input so if
the next input is mary
then it's going to do the same thing
it's going to check to see if it mary
matches the candidate list
and then it's going to increment mary's
vote by one
and then we're just going to print the
winner so first things first
we're going to work on the vote function
so the vote function is going to be
pretty simple it's going to be
mostly a big for loop
and we're going to take an int and
to keep track of the for loop and i'm
going to name it current candidate
and i'm going to copy that because it's
a pretty long
[Music]
variable name but it's just to make it
really simple for you
so we're going to keep it at zero we're
going to declare it at zero first
to start at the beginning of the list
and then as long as current
candidate i can just paste
is less than candidate
count
and then we're going to iterate for each
for each time we go through we're gonna
increment the current candidate each
time we go through the loop
so see like a lot of other programming
languages has
um functions already built into the
library and one of them is string
compare
and what we're going to do here is each
time each time we go through this
loop we're going to use string compare
and compare
the current candidate in the list that
we're looking at to the
string name here that we're given
through the vote function so up here the
user is going to
give us who they're voting for right
here
and then they're going to feed it to us
here
so name we're going to use it here
so if string compare
name is equal
is we're going to feed string compare
two
arguments it's going to be name and
candidates
current candidate dot name
and if that returns a 0 then
we know that the two
are the same that the name that the user
gave us
let's say joe matched the candidate on
the list
one of the candidates on the list so if
it's true
then we just
then we just increment candidates
by one
and then we have to return true
we return true because that way this
won't go into the invalid vote
uh if statement so that's it for the
vote
function now we're going to move on to
the print winner function
so here we're going to have to keep
track of candidate with the highest
number of votes
so to do that we're going to declare
variable
top candidate
number of votes so
we we need to keep track of the number
of votes not the candidate name because
there can be a tie
so let's say we have two people with two
votes each
then we have to be able to find both of
them
in the candidates list
so while current candidate
is less than candidate count
oh
and then we have to remember to
increment current candidate
and i like to do it before i do any of
the logic inside just i don't forget
it's a really easy thing to forget so
in within this while loop what we're
going to be doing is we're going to be
setting
the top candidate number of votes so
all we have to do is check if
top candidate number
of votes and that's actually pretty long
so i'm going to copy that one for later
use
it's less than um
candidates current
candidate
dot votes
then what we're going to do is we're
going to set it to that
we're going to set top candidate number
of votes
to candidate
to the current candidate that we're
looking at
okay then now now we we now we have this
while loop here it's going to give us
the the top
so now that we have this while loop here
it's going to give us the top candidate
number of votes
so once now that we have that number we
have to loop through the list of
candidates
and print out every time we hit that
number
we find a candidate with that number of
votes
so we're gonna use the for loop here and
since i already declared current
candidate up here
i can just
reset it right here
so as long as current candidate
is less than a candidate count
and then iterate
i know increment
i keep saying it right
so here all you have to do is check if
candidates
current candidate
votes is equal to
the top candidate number of votes
it's going to print f
string new line
candidates it's going to take from the
candidates list
at the on the current candidate
candidate that we're looking at
and we're going to take the name and
just print and plug it into the printf
that should do it let's see if i have to
have any
typos
all right so i'm going to do check 50
here to make sure
everything's good
oh and as i pressed check 50 i noticed
that that slash n was
a forward slash it needs to be a
backslash in so
i'm gonna it's gonna fail here and i'm
gonna try it again
make plurality check 50.
see like those little things can throw
it all off
all right there we go it passed and i'm
going to be providing this code in
my code phony github and if you have a
better solution you know feel free to
leave it down in the comments it helps
anyone who watches this video later on
because a lot of people check the
comments and they see a better solution
or they see something that clicks better
with them you know
and my main priority is just to make
sure that you understand what's going on
and how these basic programming concepts
come together and we
can really use all those basic tools
later on
and that's what's really important so if
you like this video hit give me a like
and subscribe maybe and i would really
appreciate it
and see you next time because it's
codepony out peace