ZAMYLA CHAN: It's a-me, Zamyla.
Today, we're going to implement Mario, where we draw Mario
a full pyramid for him to jump over.
Let's break down the sub-tasks for this problem.
>> First, we want to prompt the user for the height of the pyramid.
And then we want to make sure that that input is valid.
And then we're going to want to draw the pyramid.
>> So let's talk about prompting and validating user input.
It might look something like this, a do-while loop that
prompts the user for an integer and then only repeats
if that integer is invalid.
So what should this condition be?
>> For this, let's go back to the specification.
Well, the spec tells us that any valid height is going to be between 0 and 23,
inclusive.
So then that means that any invalid height is going to be less than 0
or more than 23.
>> So now that we have this information, let's design our condition.
But we need to be careful, because now we have
two Booleans that we want to evaluate.
>> Here I've provided you with a truth table.
This allows us to take two Booleans, Boolean one and two.
And we can evaluate either bool1 and bool2 or bool1 or bool2.
>> So what's the difference between and and or?
Well, bool1 and bool2 will evaluate to true if and only
if both Booleans are true, whereas the or operation
will be true if either one of the Booleans or both are true.
So with this in mind, see if you can figure out
what the appropriate condition for your do-while loop for an invalid n
is going to be.
>> With that, we've prompted and validated the user for the height of the pyramid
that they want.
So now, it's up to us to draw the pyramid.
The full pyramid for this problem will look a little bit
like this, where we have a left pyramid, some gap, and then a right pyramid.
So let's break this down a little bit.
>> If I go to my text editor, here I've drawn us a left-aligned pyramid.
But that won't do.
What we want to do is we want to create a right-aligned pyramid first.
So to do this, to just push my hashes along to the side,
I'm just going to put some characters in between, just these dots.
Then I'm going to put two on the next line, and one on that line.
And so here I have a right-aligned pyramid.
>> After that, I'm going to go back to the top row
and put in the gap, which is, per the spec, two spaces.
Then I'm going to fill in the other side of the pyramid.
I'm going to go to the second row, write two spaces for the gap and then two
hashes.
Back to the third row, two spaces for the gap and three hashes.
And at the end, two spaces for the gap and four hashes.
So that's what a full pyramid will look like.
>> Of course, we don't want these dots in the way.
So we're going to replace these dots by putting some spaces in.
One, two, three on the first line.
One, two on the second.
And one on the third line.
So this is what we would do if we wanted to just make a full pyramid,
say, in our text editor.
>> So let's take this, understanding the patterns,
and convert it over to some pseudocode.
For each row of the pyramid, we want to print the left pyramid and then
the gap and then the right pyramid.
For the left pyramid, we print the requisite amount of spaces,
followed by the hashes.
Then we print the gap, which is just two spaces every time.
And in the right pyramid, we print the requisite number of hashes.
>> For the second row, we conducted the same exact process.
We print the spaces for the left pyramid, the requisite amount
of hashes, the gap, two spaces, and then the hashes for the right pyramid.
>> Let's tackle the pattern for the left pyramid.
If I had, for purposes of this example, a height
of eight requested by the user, then my first row
would have one hash and seven spaces.
My second row would have two hashes, six spaces.
Third row, three hashes, five spaces.
You can probably do this yourself for a height of eight
and determine for each row how many hashes and how many spaces you need.
But what we want to do is we want to abstract it.
So I ask you for any nth row, how many hashes and how many spaces do we need?
>> Now, as you determine the pattern for how many hashes
and how many spaces you need for any nth row for a given height,
remember to be careful of how you're indexing.
What I mean by this is that in everyday life most of us start counting by one.
So the first row would be number one.
And the second row would be row number two, so on and so forth.
>> But in computer science and CS50, we are zero-indexed.
So we actually start counting at zero.
So the first row would be row number zero.
And the second row would be row number one.
So if I had a height of eight for my pyramid, then the last value of n
would actually be seven and not eight.
>> So be careful about this.
And be aware when you're determining your pattern whether your zero-indexed
or one-indexed
>> OK.
So now that we have the pattern for the left pyramid,
we need to determine the pattern for the gap.
Luckily, this is really easy.
It's just always two spaces.
>> So now we proceed to the right pattern.
The first row will have one hash.
The second row, two.
Third row, three.
So on and so forth.
So again, determine for any abstract n and any height how many hashes
and how many spaces each row should have.
>> OK.
So we know that for each row we need to conduct some process.
How do we do that?
Well, we use the for loop construct, comprised of an initialization,
a condition, and an update.
For loops can be used to repeat processes.
>> So say I want it to say hello, world 50 times, then my for loop
would look something like this, where I initialize my variable to zero.
The condition is that i is less than 50.
And then the update is that it increments by one every time.
So what this would do would be to print hello, world 50 times in a row.
>> Now, say I wanted to iterate over the height of the pyramid.
Then instead of hard coding some value in the condition,
I just use the variable height.
So what this will do is iterate over the height each row.
And I can do something inside of the body of that loop.
>> What do we want to do inside the body of the loop?
Well, as we indicated before, we want to print the spaces and the hashes
for the left pyramid and then print two spaces and then print the hashes.
So we've already figured that out.
So we can start to fill in our program a lot more.
>> Here I have an outer for loop that iterates over every row in the pyramid.
And inside that body I'm going to print spaces repeatedly,
hashes repeatedly, and then the gap, and then hashes for the right pyramid,
and then, finally, a new line to start the next row.
>> With that, we've prompted the user for the input.
We've made sure that it's valid.
And then we've drawn the pyramid.
So Mario can successfully climb the pyramid.
>> My name is Zamyla.
And this is CS50.