ZAMYLA CHAN: It's a me, Zamyla.
Today in Mario, we're going to be drawing the half pyramid for Mario
to climb up.
>> So let's talk about our to-do's for this problem.
We're going to want to prompt and validate
the user for a valid input of how high they want Mario's pyramid to be.
And then, we're going to draw it.
So let's get started with prompting and validating the user for their input.
>> We can make use of the CS50 Library Function
get int that will ensure that the user inputs an integer.
Any positive integers, negative integers, number 0 are all fair game.
Otherwise, the user will be prompted to retry until they input a valid integer.
Now although get int does a lot of the work for us
in ensuring that the user gives us an integer,
we still need to apply some additional constraints on that.
After all, we can't have Mario climbing a half pyramid of height negative 12.
>> In addition to that, the problem specification
says that we can only allow Mario to climb
a pyramid of heights between 0 and 23.
OK, so that means that we need to continuously prompt the user
to give us a valid number and only continue
once they've given us a valid height.
How do we do that?
>> Well, continuous processes give us the idea of loops-- doing something
repetitively.
One loop in C as a while loop that will continuously
execute the body of the loop as long as the given condition evaluates to true.
As soon as that condition evaluates to false,
the program will proceed to whatever comes after that.
So while loops are one way of ensuring that we continually
prompt the user for a valid input.
And once they give us a valid input, we'll proceed to whatever comes next.
We know that we're going to ask the user for input at least once.
So now we come to a sister of the while loop, which is the do while loop.
>> Do while loops will execute the body of the loop at least once.
So without checking the condition, it will execute the body of the loop.
And then check the condition to see whether it needs to repeat itself.
This comes in handy when we're validating user input.
We know that we're going to ask them at least once.
So a do while loop might look something like this.
We have an integer n.
And inside of the do while loop, we immediately
prompt the user for an integer.
If n is invalid, then we'll prompt them again and again and again until they
give us that valid integer.
Finally, once n is a valid input, we'll proceed to the rest of our program.
>> So let's go back to the spec and check what the conditions for a valid input
is going to be.
The valid heights are going to be between 0 and 23, inclusive.
So invalid heights are going to be less than 0 or more than 23.
So remember to design your condition carefully,
knowing that the condition for the do while loop
should be while n is invalid.
Now this isn't going to be a simple single Boolean expression.
We're going to have to combine two different expressions
to make our whole condition.
>> So let's just look at a truth table I've already given you the hint that we're
going to be dealing with two Booleans.
So here's a truth table where I have two Booleans-- Boolean 1 and 2.
So we have the option to evaluate bool1 and bool2 or bool1 or bool2.
And will only be true if both Booleans evaluate to true, whereas all
or will be true as long as one of the two Booleans evaluates to true.
OK, so take a moment, pause this video and digest this truth table.
I'll be right here waiting.
When you come back, see if you can piece together
a Boolean expression for your condition of n being an invalid input.
>> So now that we have valid user input, let's
go ahead and talk about how we might draw the half pyramid.
Here in this simple text editor, I've drawn a left aligned pyramid.
But we know that we need our pyramid to be right aligned.
So how might I do this?
Well, I might try to push everything to the side
by just putting a little character in between.
And then, for the next line, I'm going to put
some more characters to push it along, and further-- so on and so forth--
until I have the right aligned pyramid.
So we do have a right align pyramid, but it doesn't look so great with the dots.
But we still want to maintain that nice spacing.
So I'm going to literally insert some spaces.
>> Instead of three dots, I'll put one, two, three spaces.
On the second line.
I'll put one, two spaces.
And on the penultimate line, just one space.
And here I have a right aligned pyramid.
From doing the example in the text editor, we have an idea for the pattern
that we'll use to draw the half pyramid.
For every row, what we did is we type some spaces,
and then typed some hashes, and then typed
the Enter key, which is creating a new line.
So now that we have that, let's go one step further and find a pattern.
>> So I'm going to say, for the interest of this example,
we're dealing with a height of 8.
The first row is going to have two hashes that follows seven spaces.
The second-- three hashes, six spaces.
Third row-- four hashes, five spaces-- so on and so forth
until we get to the NTH row.
So then, I ask you for the NTH row, how many hashes are we going to have
and how many spaces?
So it's up to you to figure out a formula to represent how many hashes
and how many spaces are needed for the NTH row when you have some height.
>> Now when you're figuring this out, be careful how you're indexing.
What I mean by this is that in everyday life all of us
start counting, usually by 1.
But in CS50 and in computer science in general, we are 0 indexed.
So the first row would be n of 0 as opposed to 1.
Be careful of this when you're trying to figure out your pattern.
So now let's go back to how we're going to draw our pyramid.
For every row, we're going to want to print the spaces, print the hashes,
and then print a new line.
The hint here is the word "for" every row.
In C, we have a construct called a for loop,
which is comprised of an initialization, a condition, an update,
and the body of the loop.
>> Say I wanted to say, hello world, 50 times, my for loop
would look something like this.
I initialize my integer to 0.
The condition is that I is less than 50.
And then my update is just incrementing I by one every time.
We can also use for loops to iterate over things.
Notice here how we haven't hard coded a number,
but rather placed the variable height instead into the condition.
So what I'm doing here is I'm iterating over every row of the pyramid.
I can do something for each row inside the body of my loop.
>> What are we doing inside the body of the loop?
Well, as we already said, we're printing spaces and we're printing hashes
and we're printing a new line.
So my outer for loop will look like this.
I iterate over every row of the pyramid, using,
in this case, height as the variable that stores the height of the pyramid.
Inside the body of that loop, I'm going to print spaces repeatedly, print
the hashes repeatedly, and then print a new line.
>> So now, using all of the concepts that I've talked about in this walk-through,
you should be able to prompt the user for input, validate that input,
and then draw the half pyramid.
>> My name is Zamyla, and this is CS50.