FatherPhi video
CS50 Cash Solution - PSET 1 | 2021 Step by Step Tutorial | Live Coding
24.4K views ยท 247 likes ยท 131 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
alright guys so today as usual we're
gonna be going over the pseudocode first
for cs50 cache this is in pset one and
we're gonna hit the main problems to
address first so first you take the
input from the user and make sure that
it's a number that's very important and
takes it we take that number and print
it and print the minimum number of coins
with which changes can be made I change
can be made and then we're gonna break
down the problems even more so from
these two we're gonna break it down into
it looks like we're gonna have six total
and so we're gonna take the input for
the first we're gonna take the input
we're gonna store that input and then
we're gonna also gonna have to validate
that input so that one problem is
actually three if we break it down then
for the second problem we're gonna break
that down to three more so for that
we're gonna at the plate we're gonna
have to have a place to keep track of
the number of coins we accumulate and
while counting then we're gonna have to
take as coin specific size chunk out of
the total and for each of these coins we
have to add the track to the to a
tracking number to make sure that it
rounds and then we return the track
number and we print it to the screen
okay so let's dive into some code I'm
gonna make a new file so that we can see
but you're gonna have to name your as
cash see
so first we always have to do our
includes so include cs50.h I'm gonna
include math not H these are just the
libraries that we have to include so
that we have the correct functions to
work with or else you won't decode some
of the code we use won't work because
it's not present yet so that's going
back to that first problem we have to
take input from the user make sure that
it's number so as a broad kind of
statement we're gonna say we have to get
with taking a number from the user so
we're gonna be returning a number from a
function and let's just say it's a float
because it can be because dollars and
since like create a decimal number we
want to float so we're gonna have to ask
for positive float and it's not gonna
need anything because it's gonna take
from us you guys gonna take the input
from the user so let's make a little
section okay so I like to always declare
my variables first so we're gonna need
that in that float from positive float
that this is where we store the number
that we take from the user so then we're
also gonna need a number like a little
thing to kind of be a flag that it's
gonna be negative until proven positive
and basically it's a switch that we turn
on and off and it tells us if the number
they give us is true or not so if it's
negative or not in this case so how do
we keep asking the question over and
over like we said here let's take the
input and then validate input so to
validate it so we have we have taking
input which is the whole function itself
we're going to validate it we're
actually gonna have to take the input
inside of a validation like section so
to validate we're gonna do a while loop
let's give the while loop the negative
that a negative boolean and that's going
to be its condition so while the
negative is true which we're starting it
off is true it's gonna keep asking the
user a question and the question it's
gonna ask is give me the change owed so
this is gonna pop up to the user right
here and they're gonna know from that
prompt that they have to input the
change
owed and then this getfloat will take
that and store it as a float inside of
our positive float variable which we
declared up here if we didn't declare it
up here it would not work because it is
just the Box doesn't exist yet so now we
have to check to make sure that the
float is positive so if positive float
is greater than zero we're gonna set
negative to false so this sets to false
if our number is positive and this is a
way to interact with variables here you
can a boolean you can set to either true
or true or false and while it's true
this this whole thing will run this
thing right here this block and if we
set it to false that means it'll stop
asking the user for a float so that
means we've stored the correct data and
we can stop this and always always if we
promised to give a float to a function
we need to fulfill that promise and
return that float so here we're going to
return the variable positive float and
what this does is if we call this
function ask positive float we're going
to receive a positive float that the
user gave us okay so next the next
problem so for the second function we're
gonna tip we're gonna to take the number
from the first function and use it to
print them in a minimum number of coins
with which change can be made so from
that problem we can see that we need a
place to keep track of the number of
coins we accumulate while counting we
also need to take a coin specific chata
sized chunk out of the total for each
coin and add to the tracking number to
make sure and make sure it around and
then we return the track number and
prints it and print it to the screen
so let's just focus on keeping track of
the number of coins we came late while
counting for now
so we're gonna declare this function
rename it coin number to be simple give
it a little space so this this function
will perform the tracking of coins we
counted and returned it as an integer so
like usual I like to declare my
variables first so into total since it's
gonna be the first one and like we were
told we need to use round and we're
gonna take we're gonna use the round
function from the math dot H and we're
gonna feed it ask positive floats return
value because if you can recall from
earlier ask positive float all it does
is give you a function of float that the
user gives us so it takes the float and
then it rounds it multiplies it by 100
so that we have the number of cents and
not the number of dollars the stores
total since then we have to declare
variable to store the coin count
and for that we're gonna do another end
or name a num of coins keep it simple so
everyone can understand we're gonna have
to declare variable to store our
remainders and that's gonna be an
integer as well so hit store our
remainders because between the math that
we like between taking out chunks from
the total sense we're gonna have a
remainder that we need to store or else
it just gets lost once we do the math so
let's start it off at 0 then we're gonna
have a temp number this way you haven't
get creative sometimes in programming so
this temp number it'll become clearer
and clearer later rather than explaining
it now so declare variables to store
coin values this next so the computer
doesn't know how much a quarter is worth
so we're gonna tell it
so in quarter equal twenty five and dime
equals ten and nickel equals five and
penny equals one
so now for the math we're just basically
gonna be cascading down a list of coins
from the biggest to the smallest and
it's all been used basically the same
same formula so we're gonna start off
with an if statement so if total since
is greater than or equal to the value of
1/4 we allow it to go through this
statement right here so the equivalent
of that phrase that I just wrote the
comment is total Santo coupes total
cents is greater than or equal to water
so this is comparing the two variables
so if the total sense is the 100 that is
greater than 1/4 so it's going to go
through this code right here that we're
about to go through so first things
first we're gonna have to take remainder
and set it equal to the total since and
we're gonna use the modulo operator so
what this does is let's say we have a
hundred for total why keep diving Center
total cents let's see if I pipe it up
there No
I did mess over here and put sent okay
so the quarter is 25 and photosynthesis
100 that means we have four times that
quarter goes into total sentence and
that would set the remainder to zero and
let's make it 101 divided a modular
quarter which would give us a remainder
of one
so then we have to set a temp number and
set that equal to total since
- remainder so let's say we have a
hundred and one we're gonna subtract it
by one so we're gonna have a hundred
left
and the reason we do this is because we
have to actually find the number of
coins so next we're gonna take the
number of coins and set it equal to mum
of coins plus temp num and divide it by
quarter so this gonna take the temp and
I'm about 100 divided by the 25 of
quarter and that's gonna be four and
we're gonna add it to the number of
coins that we currently have which is
zero so a number of coins essentially
right now is zero and then we're gonna
set the total sense as equal to the
remainder so now we have the remainder
of one
equaling total cents so it's gonna keep
going down the list and doing the same
thing and it would just reject the the
dime the nickel would then at the penny
it would be like oh this is equal to one
penny so we're gonna do the same formula
again so this isn't like the most
mathematical way of doing it but as
someone with a pretty weak math
background coming up with this wasn't
didn't take me so long and I think he
can really give someone new some insight
into how like the mathematics works in
programming even if you don't have like
a PhD in math because as long as you get
the end result most of the time it's
pretty much the same
so we're going to repeat this process
for the dime
and yeah I mean I'm not supposed to
repeat myself but this is just for our
project so I'm basically just copying
the same exact thing in fact I can just
copy and paste it the whole thing
so dime dime
damn
again nickel nickel someone is cringing
so hard right now
I can guarantee you and we're gonna do
it again for penny
yeah this channel is for beginners so if
you're a C++ god or something like that
you can keep watching my content if it
if you like being iterated okay so in
the end we're gonna have to return
number of coins so after it's gone
through all this it's gonna add up the
number of coins that we've accumulated
while going through this math and spit
out this number this integer number of
coins so that's the ultimate goal so now
we have to use these two functions in
the main function so and see main is
always like this well this is one of the
ways you can do it we're just not using
some arguments right now from the
command line it's just the most basic
way to have a main function of main
function so we're gonna basically inject
this variable into a string that prints
to the console and this variable is
going to come from coin num and this is
our whole program right here when I have
my code in the description so you can
click it to check it out if you want so
face did the code over here and I'm
gonna make cash and then I'm going to
run it so let's say 120
all right so now I can go view my score
let's refresh all right we got cash i
100% new submission
okay check 50
there we go might not be the greatest
program but it didn't get rejected at
100%
style-wise - that's what's up this code
phony out thanks for watching peace
gonna be going over the pseudocode first
for cs50 cache this is in pset one and
we're gonna hit the main problems to
address first so first you take the
input from the user and make sure that
it's a number that's very important and
takes it we take that number and print
it and print the minimum number of coins
with which changes can be made I change
can be made and then we're gonna break
down the problems even more so from
these two we're gonna break it down into
it looks like we're gonna have six total
and so we're gonna take the input for
the first we're gonna take the input
we're gonna store that input and then
we're gonna also gonna have to validate
that input so that one problem is
actually three if we break it down then
for the second problem we're gonna break
that down to three more so for that
we're gonna at the plate we're gonna
have to have a place to keep track of
the number of coins we accumulate and
while counting then we're gonna have to
take as coin specific size chunk out of
the total and for each of these coins we
have to add the track to the to a
tracking number to make sure that it
rounds and then we return the track
number and we print it to the screen
okay so let's dive into some code I'm
gonna make a new file so that we can see
but you're gonna have to name your as
cash see
so first we always have to do our
includes so include cs50.h I'm gonna
include math not H these are just the
libraries that we have to include so
that we have the correct functions to
work with or else you won't decode some
of the code we use won't work because
it's not present yet so that's going
back to that first problem we have to
take input from the user make sure that
it's number so as a broad kind of
statement we're gonna say we have to get
with taking a number from the user so
we're gonna be returning a number from a
function and let's just say it's a float
because it can be because dollars and
since like create a decimal number we
want to float so we're gonna have to ask
for positive float and it's not gonna
need anything because it's gonna take
from us you guys gonna take the input
from the user so let's make a little
section okay so I like to always declare
my variables first so we're gonna need
that in that float from positive float
that this is where we store the number
that we take from the user so then we're
also gonna need a number like a little
thing to kind of be a flag that it's
gonna be negative until proven positive
and basically it's a switch that we turn
on and off and it tells us if the number
they give us is true or not so if it's
negative or not in this case so how do
we keep asking the question over and
over like we said here let's take the
input and then validate input so to
validate it so we have we have taking
input which is the whole function itself
we're going to validate it we're
actually gonna have to take the input
inside of a validation like section so
to validate we're gonna do a while loop
let's give the while loop the negative
that a negative boolean and that's going
to be its condition so while the
negative is true which we're starting it
off is true it's gonna keep asking the
user a question and the question it's
gonna ask is give me the change owed so
this is gonna pop up to the user right
here and they're gonna know from that
prompt that they have to input the
change
owed and then this getfloat will take
that and store it as a float inside of
our positive float variable which we
declared up here if we didn't declare it
up here it would not work because it is
just the Box doesn't exist yet so now we
have to check to make sure that the
float is positive so if positive float
is greater than zero we're gonna set
negative to false so this sets to false
if our number is positive and this is a
way to interact with variables here you
can a boolean you can set to either true
or true or false and while it's true
this this whole thing will run this
thing right here this block and if we
set it to false that means it'll stop
asking the user for a float so that
means we've stored the correct data and
we can stop this and always always if we
promised to give a float to a function
we need to fulfill that promise and
return that float so here we're going to
return the variable positive float and
what this does is if we call this
function ask positive float we're going
to receive a positive float that the
user gave us okay so next the next
problem so for the second function we're
gonna tip we're gonna to take the number
from the first function and use it to
print them in a minimum number of coins
with which change can be made so from
that problem we can see that we need a
place to keep track of the number of
coins we accumulate while counting we
also need to take a coin specific chata
sized chunk out of the total for each
coin and add to the tracking number to
make sure and make sure it around and
then we return the track number and
prints it and print it to the screen
so let's just focus on keeping track of
the number of coins we came late while
counting for now
so we're gonna declare this function
rename it coin number to be simple give
it a little space so this this function
will perform the tracking of coins we
counted and returned it as an integer so
like usual I like to declare my
variables first so into total since it's
gonna be the first one and like we were
told we need to use round and we're
gonna take we're gonna use the round
function from the math dot H and we're
gonna feed it ask positive floats return
value because if you can recall from
earlier ask positive float all it does
is give you a function of float that the
user gives us so it takes the float and
then it rounds it multiplies it by 100
so that we have the number of cents and
not the number of dollars the stores
total since then we have to declare
variable to store the coin count
and for that we're gonna do another end
or name a num of coins keep it simple so
everyone can understand we're gonna have
to declare variable to store our
remainders and that's gonna be an
integer as well so hit store our
remainders because between the math that
we like between taking out chunks from
the total sense we're gonna have a
remainder that we need to store or else
it just gets lost once we do the math so
let's start it off at 0 then we're gonna
have a temp number this way you haven't
get creative sometimes in programming so
this temp number it'll become clearer
and clearer later rather than explaining
it now so declare variables to store
coin values this next so the computer
doesn't know how much a quarter is worth
so we're gonna tell it
so in quarter equal twenty five and dime
equals ten and nickel equals five and
penny equals one
so now for the math we're just basically
gonna be cascading down a list of coins
from the biggest to the smallest and
it's all been used basically the same
same formula so we're gonna start off
with an if statement so if total since
is greater than or equal to the value of
1/4 we allow it to go through this
statement right here so the equivalent
of that phrase that I just wrote the
comment is total Santo coupes total
cents is greater than or equal to water
so this is comparing the two variables
so if the total sense is the 100 that is
greater than 1/4 so it's going to go
through this code right here that we're
about to go through so first things
first we're gonna have to take remainder
and set it equal to the total since and
we're gonna use the modulo operator so
what this does is let's say we have a
hundred for total why keep diving Center
total cents let's see if I pipe it up
there No
I did mess over here and put sent okay
so the quarter is 25 and photosynthesis
100 that means we have four times that
quarter goes into total sentence and
that would set the remainder to zero and
let's make it 101 divided a modular
quarter which would give us a remainder
of one
so then we have to set a temp number and
set that equal to total since
- remainder so let's say we have a
hundred and one we're gonna subtract it
by one so we're gonna have a hundred
left
and the reason we do this is because we
have to actually find the number of
coins so next we're gonna take the
number of coins and set it equal to mum
of coins plus temp num and divide it by
quarter so this gonna take the temp and
I'm about 100 divided by the 25 of
quarter and that's gonna be four and
we're gonna add it to the number of
coins that we currently have which is
zero so a number of coins essentially
right now is zero and then we're gonna
set the total sense as equal to the
remainder so now we have the remainder
of one
equaling total cents so it's gonna keep
going down the list and doing the same
thing and it would just reject the the
dime the nickel would then at the penny
it would be like oh this is equal to one
penny so we're gonna do the same formula
again so this isn't like the most
mathematical way of doing it but as
someone with a pretty weak math
background coming up with this wasn't
didn't take me so long and I think he
can really give someone new some insight
into how like the mathematics works in
programming even if you don't have like
a PhD in math because as long as you get
the end result most of the time it's
pretty much the same
so we're going to repeat this process
for the dime
and yeah I mean I'm not supposed to
repeat myself but this is just for our
project so I'm basically just copying
the same exact thing in fact I can just
copy and paste it the whole thing
so dime dime
damn
again nickel nickel someone is cringing
so hard right now
I can guarantee you and we're gonna do
it again for penny
yeah this channel is for beginners so if
you're a C++ god or something like that
you can keep watching my content if it
if you like being iterated okay so in
the end we're gonna have to return
number of coins so after it's gone
through all this it's gonna add up the
number of coins that we've accumulated
while going through this math and spit
out this number this integer number of
coins so that's the ultimate goal so now
we have to use these two functions in
the main function so and see main is
always like this well this is one of the
ways you can do it we're just not using
some arguments right now from the
command line it's just the most basic
way to have a main function of main
function so we're gonna basically inject
this variable into a string that prints
to the console and this variable is
going to come from coin num and this is
our whole program right here when I have
my code in the description so you can
click it to check it out if you want so
face did the code over here and I'm
gonna make cash and then I'm going to
run it so let's say 120
all right so now I can go view my score
let's refresh all right we got cash i
100% new submission
okay check 50
there we go might not be the greatest
program but it didn't get rejected at
100%
style-wise - that's what's up this code
phony out thanks for watching peace