FatherPhi video
CS50 Mario Solution - PSET 1 | 2021 Step by Step Tutorial | Live Coding
11.5K views ยท 71 likes ยท 20 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
today we'll be doing an exercise and
problem set one from cs50 we're going to
be handling the Mario Moore project
we're going to go line by line and
explain my solution to this problem stay
tuned
so we're gonna start off by looking at
the requirements
so we're joining Mario more p-set we're
going to be printing these blocks so
it's gonna be two two pyramids aligned
to the middle made of hash blocks so
it's gonna be one line asking the height
we didn't put the height and then it's
gonna be however many like if they put
an eighth it's gonna be eight rows tall
[Music]
and it's gonna you know it's gonna do
the same thing for four except for same
thing for two so on and apparently they
only want us to include the numbers
between one and eight including one and
eight and notice there's a gap between
the pyramids so we're gonna have to
print that too when we test the code is
gonna have to kick out negative numbers
zero anything that's not between one and
eight including letters or if they don't
put anything at all it's gonna have to
repeat and we asked us the question so
winning over some pseudocode this the
steps I usually take to get to the code
so for the requirements we're having to
test for the correct input that's gonna
be the first thing second we have to
print two symmetrical pyramids based on
input with a gap in the middle they
includes that we're gonna have to
include which is the code then we have
to bring in from the outside of the coat
our quote our own code it's gonna be
cs50.h which is cs50 library and then
standard i/o not age which is I guess
the standard yeah library for for see
and from here we're gonna break down
those first two problems into as many as
we can so I see three main problems so
basically the first one is gonna remain
the same testing for input we're gonna
ask a user for an input which is
validated which I guess could be a
separate one but I'm gonna
find those into one function then we're
gonna have to print each row for the
full height of the pyramid so since for
example if we have eight rows we have to
print each row individually and then
we're gonna have to print the correct
amount of blocks on each row so I think
that's a step in itself printing the row
and then also printing the individual
rows are separate problems in my
imagination so let's go to a new file
and I'm gonna code this line by line
when you're looking to the left because
I pre-made this to make sure it works so
first we're gonna bring in libraries do
this we're gonna have that hash we have
two hash tag include them and have two
so that's cs50.h and standard IOH and
then I like to try to play around with
my prototypes first so function
prototypes this allows you to declare
the prod of the functions ahead of time
so you know what is in your program then
actually cluttering the top of the
program up with the actual functions how
we can just go to stage of the main and
it increases readability so we're gonna
go with a spike first so this is gonna
the user for their height and then
what's it gonna take
is it gonna take any arguments no I
don't think so because it's just gonna
ask the user for something so that's
that's a function within it so we're
gonna and that doesn't require us to
feed it anything this is in a return an
integer because the height is a number
and then after that so that's gonna be
the prototype of a spike next we're
gonna do print row because I think we
have to focus on the rows first like
good from the big to the small so we're
gonna do the row and that's not gonna
return anything that that's
gonna print and we're gonna need a print
row and that's gonna take the height I
think because that's just it's just
determining how many rows to print and
it's gonna print them based on the
height number but then within the print
rail we're gonna have a print block
which is also just printing stuff it's
not gonna return anything so I'm just
gonna do void and for this we're gonna
need to know what to print so it blocks
within the row we need to know the
length of the room let's do int roll em
it's a mean number that shows us the
length of the room and then we're gonna
have to tell it which row we're on so
that it knows with how many blocks to
print so I'm gonna do it thrown number
leaves the things we win up we're going
to feed it for print block to work so
I'm going to talk about the main
function that delegate the work to other
functions so I'm just going to clear
here so we can go back to it later so
[Music]
the first function is going to be an
effect that's pipe it's gonna ask the
user X the end user for I'm just gonna
repeat until the user gives us over 1
which is positive in here between 1 and
8 including one an 8 so you're gonna
retype the prototype so this is gonna be
better for the function does a bracket
staying close to function so what are we
gonna need for ask height I usually like
to declare my variables first and so
obviously we're gonna have to have a
height payable to store the height
whenever we
the user is gonna be in tight it's gonna
store user input and then we're gonna
have to you know check that for validity
you know what I mean so we've got these
boolean here I'm going to set sort of
variable to incorrect input and it's
gonna be true for now because right now
we have no input so it's gonna be it's
gonna be true it's the input is
incorrect and since we have to keep
asking the user for input we're gonna
before we actually ask them to them so
we're gonna open up a loyal do that's
what I want to use you can use anything
you want and we're gonna set the
condition too as long as incorrect input
is true which means the input is
incorrect
it'll keep running as long as the input
is incorrect
so you're actually gonna call the
function
as for user input we're gonna pull a
function from the cs50 library called
get in it it was going to take an
integer so first I'm gonna print out
this question we're gonna ask them
whether for a height and it's going to
storage in the height variable that we
declared earlier okay so now that we've
done it's gonna take the height from the
user well the number that they
supposedly the number that the user
gives us and then it's gonna wait to
check it right is greater than zero
but also the height has to be less than
nine like these operators don't include
the numbers that we actually compare it
to so it's gonna give this the set
between one and eight including one
eight but this is true he'll set the
incorrect in third boolean
the thoughts telling the wild loop that
the input is no longer incorrect so it
can break out of it
so it's gonna repeat until it sets in
correct input to false which means that
the the height is correct but then also
we have to remember like we feel done
here but we have to always remember to
return what we promised so here we say
we're going to return an integer so we
have to actually do that whatever I did
an integer so that'll that'll do it to
the neck next we're gonna move on to I'm
gonna do print block first because
whenever I'm planning I like to do big
small but whenever I'm actually doing
the work like programming I like to
start from the small and then work my
way up to the big so actually cut that
part and I'm going to mix I'm gonna do
print really is gonna return nothing so
it's a boys I'm gonna straight throw in
height we're feeding it the integer okay
so read poem it's gonna be the first
variable that we declare
which star dead zeros we're gonna start
the first one and then how do we keep
doing this for the entire height so I'm
going to use a while loop this loop call
it's gonna call print block over and
over until the roads are alternate
[Music]
broken is less than this doctors here
say will not be included hugging so here
we are gonna call pin block which we're
gonna create right after this we give it
the height because we said earlier well
it's defined this way let's put that in
later if you know for sure what these
numbers are gonna be you do know we're
gonna shift the real town
and here we return nothing we promise
there nothing so we return nothing it's
just me printing there she's going to be
just calling this function but like I
said earlier we're gonna have a void
print block print block is gonna
actually be printing and what i
returning anything and it takes an if it
takes two integers it's gonna take over
it's going to take real number so we can
go back to this function up here and
then feed it real number let me Rowlands
would probably be the height right so as
I'll tell us how long the rows would be
including the empty blocks so whenever
we're printing these rows we're actually
printing the entire block entire row but
we just have to differentiate between
empty blocks which are spaces and hash
blocks which are hash tags so the total
length of that would be the height and
then that's if you get the row number
which is which row we're focusing on
which would probably be no count so
select print print block now that we
have that set up first we're going to
need to figure out what variables we
need so I'm gonna I set the focus first
see which block we're focusing on them
focusing on I'm gonna name it block
count count which block where and I said
earlier we're gonna have empty blocks
we're gonna have full blocks so let's
just make empty block box don't know yet
and football so let's just think about
this for a second to get the the number
of spaces we're gonna need to find out
so to know how many blocks are printing
we're gonna have to use the row number
cuz row number is gonna tell us how many
blocks are needed in each pyramid let's
say we're focused on Row 2
we're only printing two blocks for a
pyramid so to figure out the empty
blocks it's just the inverse so we're
gonna subtract and then we're gonna have
to subtract one from that because
they're starting a zero with block count
then full blocks are just inverse of
that which is rollin - empty blocks so
from this we figured out the number of
empty blocks with nerve number full
blocks but this doesn't seem to solve
the other side of the pyramid this
solves one sided pyramid to do the other
side we need to figure out how long the
full row is because I don't want to
print spaces on the other side I'm just
going to stop once I finish the blocks
there are hash hash tags so another hint
for in full row Flint so for this one is
just going to be rollin + full blocks
because we're just repeating the same
process on the other side
yeah I don't need a plus-one here we're
just gonna do that so there to reiterate
empty box it is for printing the spaces
full boxes the number of actual hashtags
were printing and then full roll length
is the combined length of both pyramids
so keep doing this for the whole row we
have to do a loop so I love while loops
so we're gonna do a while loop here and
we're gonna do block as long as clock
count is less than full roll in even
though there's a gap in the middle I'm
just gonna add that to a print statement
a print statement is and it's really not
giving any difference for printing a
regular block because I'm just gonna add
on a space to normal and have a
condition so let's make that the first
condition so if the block count is equal
to the rollin minus 1 since we started
here oh we're going to print and we're
gonna we don't print a hash tag but
because this is after the fit right
after the first period this is checking
for the condition if we're focusing on
the block grab putting act because we're
not really Lim minus 1 that means we're
at the end of the first experiment so
like let's say we're at - all right and
right now our role in our block out is
that - we're going to have to print a
little gap so they're important all
right after this we're gonna have to
shift the focus to the next block the
block count plus plus that's gonna add
one to the block count after this we're
gonna check for another condition else
if checks for the only checks to this
condition if the first one was not bent
so after the first after that we're
gonna add condition which is walk count
people equal to listing full roll length
this time and subtract one from it so
well this is going to check for is if
we'd actually reach the end of the
second pyramid and this way we don't
have to what we're gonna go down to the
next on a stick because that's the goods
it's gonna break the loop anyway because
it's minus 1 that means the next one is
going to be equal to rollin and it's not
gonna continue the while loop we're
gonna print up and when it has the hash
tag and the newline and again very
important that the shift the ball count
to the next after then we're gonna check
for another condition you know we need
to check the seat for printing an ad
block or the full dog so if our empty
block number is greater than the block
count that means we're still printing
because they're starting off printing
empty box so as long as the empty block
count is greater than don't count that
means we're still on focusing on an
empty block so here we're just gonna
print their eggs and of course we have
to shift the block count so on and this
again only executes is the other two
failed for the last condition we're not
gonna have to set a condition we're just
going to say this is the default thing
if the other ones don't work we're gonna
print hash tag and then again
breaks at the end of the full rollin so
now that we have this function written
out we know what to do for the main the
main function we have all three of our
our functions that we talked about
written out so we're going to go into
the main function and we're gonna start
calling you it's calling the functions
so first things first we do that number
you have to feed it to print row so I'm
gonna put a sky then print row cuz a sky
really is just the eight so then once we
feed print row the eight it does the
rest of the work and feeds print block
the numbers that it has and then it does
it really does all the work there so
let's see if this works
to do this I'm going to copy this and
put it in my I'm gonna paste it in here
so this is what we just wrote
thanks for watching through or skipping
to this point in the video if you like
if you like my content like or subscribe
and I would appreciate it very much
thanks
problem set one from cs50 we're going to
be handling the Mario Moore project
we're going to go line by line and
explain my solution to this problem stay
tuned
so we're gonna start off by looking at
the requirements
so we're joining Mario more p-set we're
going to be printing these blocks so
it's gonna be two two pyramids aligned
to the middle made of hash blocks so
it's gonna be one line asking the height
we didn't put the height and then it's
gonna be however many like if they put
an eighth it's gonna be eight rows tall
[Music]
and it's gonna you know it's gonna do
the same thing for four except for same
thing for two so on and apparently they
only want us to include the numbers
between one and eight including one and
eight and notice there's a gap between
the pyramids so we're gonna have to
print that too when we test the code is
gonna have to kick out negative numbers
zero anything that's not between one and
eight including letters or if they don't
put anything at all it's gonna have to
repeat and we asked us the question so
winning over some pseudocode this the
steps I usually take to get to the code
so for the requirements we're having to
test for the correct input that's gonna
be the first thing second we have to
print two symmetrical pyramids based on
input with a gap in the middle they
includes that we're gonna have to
include which is the code then we have
to bring in from the outside of the coat
our quote our own code it's gonna be
cs50.h which is cs50 library and then
standard i/o not age which is I guess
the standard yeah library for for see
and from here we're gonna break down
those first two problems into as many as
we can so I see three main problems so
basically the first one is gonna remain
the same testing for input we're gonna
ask a user for an input which is
validated which I guess could be a
separate one but I'm gonna
find those into one function then we're
gonna have to print each row for the
full height of the pyramid so since for
example if we have eight rows we have to
print each row individually and then
we're gonna have to print the correct
amount of blocks on each row so I think
that's a step in itself printing the row
and then also printing the individual
rows are separate problems in my
imagination so let's go to a new file
and I'm gonna code this line by line
when you're looking to the left because
I pre-made this to make sure it works so
first we're gonna bring in libraries do
this we're gonna have that hash we have
two hash tag include them and have two
so that's cs50.h and standard IOH and
then I like to try to play around with
my prototypes first so function
prototypes this allows you to declare
the prod of the functions ahead of time
so you know what is in your program then
actually cluttering the top of the
program up with the actual functions how
we can just go to stage of the main and
it increases readability so we're gonna
go with a spike first so this is gonna
the user for their height and then
what's it gonna take
is it gonna take any arguments no I
don't think so because it's just gonna
ask the user for something so that's
that's a function within it so we're
gonna and that doesn't require us to
feed it anything this is in a return an
integer because the height is a number
and then after that so that's gonna be
the prototype of a spike next we're
gonna do print row because I think we
have to focus on the rows first like
good from the big to the small so we're
gonna do the row and that's not gonna
return anything that that's
gonna print and we're gonna need a print
row and that's gonna take the height I
think because that's just it's just
determining how many rows to print and
it's gonna print them based on the
height number but then within the print
rail we're gonna have a print block
which is also just printing stuff it's
not gonna return anything so I'm just
gonna do void and for this we're gonna
need to know what to print so it blocks
within the row we need to know the
length of the room let's do int roll em
it's a mean number that shows us the
length of the room and then we're gonna
have to tell it which row we're on so
that it knows with how many blocks to
print so I'm gonna do it thrown number
leaves the things we win up we're going
to feed it for print block to work so
I'm going to talk about the main
function that delegate the work to other
functions so I'm just going to clear
here so we can go back to it later so
[Music]
the first function is going to be an
effect that's pipe it's gonna ask the
user X the end user for I'm just gonna
repeat until the user gives us over 1
which is positive in here between 1 and
8 including one an 8 so you're gonna
retype the prototype so this is gonna be
better for the function does a bracket
staying close to function so what are we
gonna need for ask height I usually like
to declare my variables first and so
obviously we're gonna have to have a
height payable to store the height
whenever we
the user is gonna be in tight it's gonna
store user input and then we're gonna
have to you know check that for validity
you know what I mean so we've got these
boolean here I'm going to set sort of
variable to incorrect input and it's
gonna be true for now because right now
we have no input so it's gonna be it's
gonna be true it's the input is
incorrect and since we have to keep
asking the user for input we're gonna
before we actually ask them to them so
we're gonna open up a loyal do that's
what I want to use you can use anything
you want and we're gonna set the
condition too as long as incorrect input
is true which means the input is
incorrect
it'll keep running as long as the input
is incorrect
so you're actually gonna call the
function
as for user input we're gonna pull a
function from the cs50 library called
get in it it was going to take an
integer so first I'm gonna print out
this question we're gonna ask them
whether for a height and it's going to
storage in the height variable that we
declared earlier okay so now that we've
done it's gonna take the height from the
user well the number that they
supposedly the number that the user
gives us and then it's gonna wait to
check it right is greater than zero
but also the height has to be less than
nine like these operators don't include
the numbers that we actually compare it
to so it's gonna give this the set
between one and eight including one
eight but this is true he'll set the
incorrect in third boolean
the thoughts telling the wild loop that
the input is no longer incorrect so it
can break out of it
so it's gonna repeat until it sets in
correct input to false which means that
the the height is correct but then also
we have to remember like we feel done
here but we have to always remember to
return what we promised so here we say
we're going to return an integer so we
have to actually do that whatever I did
an integer so that'll that'll do it to
the neck next we're gonna move on to I'm
gonna do print block first because
whenever I'm planning I like to do big
small but whenever I'm actually doing
the work like programming I like to
start from the small and then work my
way up to the big so actually cut that
part and I'm going to mix I'm gonna do
print really is gonna return nothing so
it's a boys I'm gonna straight throw in
height we're feeding it the integer okay
so read poem it's gonna be the first
variable that we declare
which star dead zeros we're gonna start
the first one and then how do we keep
doing this for the entire height so I'm
going to use a while loop this loop call
it's gonna call print block over and
over until the roads are alternate
[Music]
broken is less than this doctors here
say will not be included hugging so here
we are gonna call pin block which we're
gonna create right after this we give it
the height because we said earlier well
it's defined this way let's put that in
later if you know for sure what these
numbers are gonna be you do know we're
gonna shift the real town
and here we return nothing we promise
there nothing so we return nothing it's
just me printing there she's going to be
just calling this function but like I
said earlier we're gonna have a void
print block print block is gonna
actually be printing and what i
returning anything and it takes an if it
takes two integers it's gonna take over
it's going to take real number so we can
go back to this function up here and
then feed it real number let me Rowlands
would probably be the height right so as
I'll tell us how long the rows would be
including the empty blocks so whenever
we're printing these rows we're actually
printing the entire block entire row but
we just have to differentiate between
empty blocks which are spaces and hash
blocks which are hash tags so the total
length of that would be the height and
then that's if you get the row number
which is which row we're focusing on
which would probably be no count so
select print print block now that we
have that set up first we're going to
need to figure out what variables we
need so I'm gonna I set the focus first
see which block we're focusing on them
focusing on I'm gonna name it block
count count which block where and I said
earlier we're gonna have empty blocks
we're gonna have full blocks so let's
just make empty block box don't know yet
and football so let's just think about
this for a second to get the the number
of spaces we're gonna need to find out
so to know how many blocks are printing
we're gonna have to use the row number
cuz row number is gonna tell us how many
blocks are needed in each pyramid let's
say we're focused on Row 2
we're only printing two blocks for a
pyramid so to figure out the empty
blocks it's just the inverse so we're
gonna subtract and then we're gonna have
to subtract one from that because
they're starting a zero with block count
then full blocks are just inverse of
that which is rollin - empty blocks so
from this we figured out the number of
empty blocks with nerve number full
blocks but this doesn't seem to solve
the other side of the pyramid this
solves one sided pyramid to do the other
side we need to figure out how long the
full row is because I don't want to
print spaces on the other side I'm just
going to stop once I finish the blocks
there are hash hash tags so another hint
for in full row Flint so for this one is
just going to be rollin + full blocks
because we're just repeating the same
process on the other side
yeah I don't need a plus-one here we're
just gonna do that so there to reiterate
empty box it is for printing the spaces
full boxes the number of actual hashtags
were printing and then full roll length
is the combined length of both pyramids
so keep doing this for the whole row we
have to do a loop so I love while loops
so we're gonna do a while loop here and
we're gonna do block as long as clock
count is less than full roll in even
though there's a gap in the middle I'm
just gonna add that to a print statement
a print statement is and it's really not
giving any difference for printing a
regular block because I'm just gonna add
on a space to normal and have a
condition so let's make that the first
condition so if the block count is equal
to the rollin minus 1 since we started
here oh we're going to print and we're
gonna we don't print a hash tag but
because this is after the fit right
after the first period this is checking
for the condition if we're focusing on
the block grab putting act because we're
not really Lim minus 1 that means we're
at the end of the first experiment so
like let's say we're at - all right and
right now our role in our block out is
that - we're going to have to print a
little gap so they're important all
right after this we're gonna have to
shift the focus to the next block the
block count plus plus that's gonna add
one to the block count after this we're
gonna check for another condition else
if checks for the only checks to this
condition if the first one was not bent
so after the first after that we're
gonna add condition which is walk count
people equal to listing full roll length
this time and subtract one from it so
well this is going to check for is if
we'd actually reach the end of the
second pyramid and this way we don't
have to what we're gonna go down to the
next on a stick because that's the goods
it's gonna break the loop anyway because
it's minus 1 that means the next one is
going to be equal to rollin and it's not
gonna continue the while loop we're
gonna print up and when it has the hash
tag and the newline and again very
important that the shift the ball count
to the next after then we're gonna check
for another condition you know we need
to check the seat for printing an ad
block or the full dog so if our empty
block number is greater than the block
count that means we're still printing
because they're starting off printing
empty box so as long as the empty block
count is greater than don't count that
means we're still on focusing on an
empty block so here we're just gonna
print their eggs and of course we have
to shift the block count so on and this
again only executes is the other two
failed for the last condition we're not
gonna have to set a condition we're just
going to say this is the default thing
if the other ones don't work we're gonna
print hash tag and then again
breaks at the end of the full rollin so
now that we have this function written
out we know what to do for the main the
main function we have all three of our
our functions that we talked about
written out so we're going to go into
the main function and we're gonna start
calling you it's calling the functions
so first things first we do that number
you have to feed it to print row so I'm
gonna put a sky then print row cuz a sky
really is just the eight so then once we
feed print row the eight it does the
rest of the work and feeds print block
the numbers that it has and then it does
it really does all the work there so
let's see if this works
to do this I'm going to copy this and
put it in my I'm gonna paste it in here
so this is what we just wrote
thanks for watching through or skipping
to this point in the video if you like
if you like my content like or subscribe
and I would appreciate it very much
thanks