BRIAN: In Mario, your task is going to be to recreate
a pyramid from Super Mario Brothers.
So in the game Super Mario Brothers, you might
recall a pyramid that looks a little something like this.
And your task in this problem is going to be
to recreate that pyramid using hashes on the screen.
So the pyramid, for example, might look a little something like this.
And to make things a little bit more interesting,
we're going to let the user choose how tall or short
they want the pyramid to be.
The user should be allowed to input any height between one and eight inclusive
and see this pyramid displayed to the screen of the appropriate height.
So for example, if you run the program dot slash Mario,
the program should prompt the user to type in a height.
If they type in height of eight, for example,
they should see a pyramid of height 8 just like this appear on the screen.
If the user were to instead run dot slash Mario
and then type and a height of three, then this is the pyramid
should result, the same pyramid but with a height of three instead of eight.
But you should make sure to only allow heights between one
and eight inclusive as inputs to the Mario program.
If someone were to run dot slash Mario, for example,
but provided invalid input, like negative 1, for example,
then your program should reprompt to the user to type in another height.
If they type in zero, for example, also not a valid height,
you should reprompt.
And if the type a number that's too big, you should also reprompt.
And only when they give you a number between one and eight
inclusive, should your program then display the resulting pyramid.
So the first thing you might want to do is
try writing some pseudocode to describe in English like syntax
what your program should do in order to implement the desired behavior.
The first thing that you might imagine that your program is going to do
is prompting the user for input.
They need to type in the height of the pyramid that they want.
In order to do that, you can take advantage of a function
in the CS50 library called Get Int.
The Get Int function will prompt the user to type in an integer as input.
And the Get Int function will automatically
take care of handling if the user types in something that isn't an integer.
If the user types in a character or a word,
for example, Get Int will handle rejecting that input
and prompting the user to type in something again.
What you'll need to do is add some additional logic
to make sure that the height is within a valid range.
You only want to accept heights between one and eight inclusive.
And if the height the user types in is outside of that range,
then you'll want to reprompt the user to type in another height.
So your task is going to be to check.
If the input is invalid, we should reprompt the user for another input.
And this idea of doing something potentially again and again and again,
prompting the user one more time, one more time, might remind you of a loop.
And so we could think about using a loop to solve this problem.
For example, here is a while loop, an example
of a loop that will continue to repeat a block of code
so long as a particular condition is true.
We check the condition.
If it's true, then we run the code.
And this will result in repeating this block of code zero or more times.
Because if the condition is false, then we're not going to run the loop at all.
But in Mario, we know that we're always going to run this loop at least once.
We always want to prompt the user at least once to type in a height.
And if the height is invalid, then we might end up prompting the user again.
So to do that, we might try to use a sister of the while loop,
the do while loop.
Very similar in spirit, the only difference
here is that we're always going to run this block of code at least once.
And then we're going to check the condition.
And if the condition is true, then we're going to run this loop again and again
and again.
So how could you actually use a do while loop?
Well, here's an example of a do while loop that gets a positive integer.
We first declare a variable called n.
And here's our do while loop.
We're going to repeat this block of code,
letting n equal Get Int positive number, meaning
we're going to prompt the user to type in a positive number,
and save whatever they type in inside of the variable n.
And we're going to keep repeating this while n is less than one.
In other words, after we've run this code, we're going to check,
is n less than one?
In other words, is it not a positive integer because it's less than one?
And if so, then we should reprompt the user asking them
to type in a positive number again and keep
doing this over and over and over until we get a positive integer.
Of course, the case of Mario, you don't want a prompt
for just a positive integer.
You want to prompt for an integer in the range of one to eight.
So you want to think about what you could fill into that condition
in order to get this do while loop to behave the way you would expect it to.
What you're going to fill into this condition
is what's called a Boolean expression, an expression in programming that
is either true or false.
And Boolean expressions have a number of different varieties.
You could check if a variable is equal to something.
Here, for example, is a Boolean expression
that checks if x is equal to zero.
Boolean expressions can also be things like, is x less than zero
or is x greater than zero?
And you can even take two or more Boolean expressions
and combine them together.
Here, for example, these two vertical bars mean or.
So this expression is true if x is zero or x is one.
And these two ampersand symbols mean and.
In other words, this last Boolean expression
will be true only if x is greater than zero and x is less than 10.
So think about what Boolean expression you might use inside of your
do while loop to make sure that the input is between one and eight.
And if it's not, you should reprompt the user to type in another height.
Once you've gotten the user to input a valid height,
the next step is going to be actually building this pyramid.
And it turns out, it's actually easier to build the opposite
of the pyramid you're trying to build.
So here's the pyramid you're trying to build
where everything is right aligned.
But it turns out, it's actually a little bit easier
to build a pyramid that is left aligned instead.
So let's start by trying to build this left aligned pyramid.
What you might notice is that we have a number of rows.
And on each row, we have some number of hashes.
And so we're probably going to repeat some code for every row in this pyramid
printing out the appropriate number of hashes.
To do that, we can use a different kind of loop called a for loop.
A for loop repeats a certain block of code some number of times.
And how many times that code repeats depends upon three parts.
The first part of the for loop is called the initialization.
This is where we initialize or create a variable or variables.
And in this case, we'll initialize a variable
that's going to keep track of how many times our loop has run so far.
The next part of the for loop is called the condition.
The condition is a Boolean expression that answers the question of,
should we continue to run this loop?
Every time we get to the next iteration of the loop,
we're first going to check the condition.
And if the condition is true, then we're going to run the loop again.
But if the condition is false, then we're going to stop.
And finally, we have an update, which is what
code runs at the end of every iteration of the loop.
And ideally, this is probably going to be updating that variable that we
set up in the initialization step.
So what might this look like?
Well, if I wanted some code that printed hello 10 times, for example,
the code might look a little something like this.
We first initialize a counting variable that we're
going to call i and set it equal to zero initially.
And we're going to keep repeating this for loop as long as i is less than 10.
And what's the update step?
Well, at the end of every iteration of the loop,
we're running i plus plus, which means take the value of i and increment it
or increase it by one.
So the first time the loop runs, i is equal to zero, the second time i
is equal to one, so on and so forth until i is equal to 10.
At which point, the condition fails because 10 is not less than 10.
And so we exit our for loop.
So this code will print hello 10 times.
So what do you want to do?
Well, you also probably want a for loop.
But for each row rather than printing hello,
you're going to want to print out the appropriate number of hashes
and then print out a new line.
How many hashes do you want to print out?
Well, on the first row of the pyramid, you probably want one hash.
On the second row, you want two hashes so on and so forth up
until the entire height of that pyramid.
So once you do that, you should be able to compile and run your Mario program
and see a left aligned pyramid of a height of one, two, three, four, five,
six, seven, or eight.
But that's a left aligned pyramid.
And what we're going for is a right aligned pyramid.
So the next step is going to be to right align this pyramid by adding some dots.
And what I mean by that is that, right now, our pyramid
looks a little something like this.
Here's a pyramid of height seven that's entirely left aligned.
What we're going to do now is update our Mario dot see program
to right align it by adding some dots along the left side of the rows
such that the entire pyramid is now right aligned.
What change does this require inside of your Mario dot see program?
Well, right now, for every row, you're printing out some number of hashes,
and then you're printing out a new line.
What you'll need to change here is that before you print out those hashes,
you'll need to print out the appropriate number of dots.
Of course, the natural question here is, how many dots do you print out?
And how many hashes do you print out?
Well, ultimately, that's going to depend upon two things.
It's going to depend first on the height of the pyramid.
And it's going to depend second on what row number you're currently on.
Let me give you an example.
Here, we have a height of seven.
And how many dots and hashes are in each row?
Well, in the first row, which I'll call row zero, because in computer science,
we often zero index things so that the first row is called row zero,
we have six dots and then one hash mark.
In row one, we have five dots and two hash marks.
In row two, we have four dots and three hash marks, so on and so forth.
And you can draw this sort of diagram for pyramids of other heights as well.
Try one for a height of three, for example, or a height of eight
and see if you can come up with some sort of pattern or some sort of formula
where you can take the height of the pyramid and the row number
to figure out how many dots you need in that row
and how many hashes you need in that row.
Once you're able to implement that, you should
be able to run your Mario program and see this right aligned pyramid
with dots in order to shift all of the rows to the right.
Finally, the last step of Mario is just going to be removing those dots.
That right now, your pyramid looks a little something
like this with a right aligned hash mark pyramid and dots along the left side.
And what you'll need to do is just modify
Mario dot see to replace those dots with spaces
so that you end up with a right aligned pyramid
that looks the way the pyramid from Super Mario Brothers also looked.
Once you've done that, you should be able to test your program.
Compile your program by running make Mario.
And then when you run dot slash Mario, you should be able to type and a height
and see the resulting right aligned pyramid.
My name is Brian, and this was Mario.