>> [MUSIC PLAYING]
>> ROB BOWDEN: It's me, Rob.
Let's walk through how to implement Mario.
So the first thing we need to do is prompt the user for the input.
>> We need to ask them exactly how tall the pyramid should be.
So here, we see we're doing height = GetInt.
An And remember that the GetInt function is implemented in the CS50
library, so up top, we need to remember to #include cs50.h.
>> So why do we have this wrapped in a do-while loop?
Well, we also need to remember that the user's input has to be valid.
What is an invalid input?
Well the pset spec specifically says that a height less than 0 or greater
than 23 is invalid.
>> So up here, we see that we're defining a constant called MAX_HEIGHT with the
value of 23.
This do-while loop will continue while height is less than 0 or MAX_HEIGHT is
less than height, which means that 23 is less than the height.
So if height is 24 or greater, we're going to continue looping.
Remember that do-while loops are pretty helpful whenever we want to get
user input and then validate it, since we inevitably need to ask the user at
least once for the value they want.
>> So once we have their input, we can now build the pyramid.
One of the tricks of this problem set is that we have to start at the top of
the pyramid.
You can't printf the bottom of the pyramid and then build your way up.
So let's look at the example from the pset spec.
>> We see here that when we enter a height of 8, the very bottom of the
pyramid prints nine hashes.
One level up from that prints one space and eight hashes.
One level up from that is two spaces and seven hashes, all the way until we
get to the top of the pyramid, which is eight levels up, which prints seven
spaces and two hashes.
So remember that we have to do this top level first.
>> Here we're iterating from the top level, row 8, continuing
until row reaches 0.
So how many spaces did we need to print in that top row?
We printed seven spaces and two hashes.
So the number of spaces we want is the row that were on minus 1.
>> If the top row is 8, 8 minus 1 gives us seven spaces.
Then we have a loop that will print out each space one at a time.
So when spaces is 7, this loop seven times, printing
seven individual spaces.
>> So now we need to print these hashes at the end of the pyramid.
So here, we need to calculate the number of hashes.
We see that we're doing height minus row plus 2.
So how did we get that?
>> Remember that the top of the pyramid is row 8, and the height is 8.
And we still printed two hashes.
So at the very least, 8 minus 8 plus 2 gives us the right answer.
And then consider the bottom of the pyramid, row 1.
Height minus row will give us 7, and then plus 2 gives us nine hashes,
which is exactly the number of hashes that we printed.
So this is the formula we want to use to calculate the number
hashes in each row.
>> Using that number, we then have another for loop, very similar to the
for loop that we used for the spaces, that iterates number of hashes times
printing a single hash each time.
On the top row, that'll print two hashes.
On the bottom row, that'll print nine hashes.
And each other row will print every number of hashes in between.
>> And then at the very end, we need to print our new line to go to the next
row in the pyramid.
Finally, we need to print the new line at the end of the row in order to
continue to the next row of the pyramid.
And at the end of our program, we have the return 0.
>> As per the pset spec, the return 0 isn't strictly necessary.
But it does signify that main is done.
My name is Rob, and this was Mario.
>> [MUSIC PLAYING]