BRIAN: In Mario, your task is going to be
to re-implement some pyramids from the game Super Mario Brothers
in code in a C program.
So we're going to take this stage in Super Mario Brothers
and take those two pyramids and represent
them using hash marks on the screen.
So you'll want to create a pyramid that looks a little something like this,
with a left pyramid and a right pyramid and a gap in between them.
How should your program work?
Well, if the user runs a ./Mario, they should be prompted to type in a height,
any number between 1 and 8 inclusive.
If they type in the number 8, for example,
this is what should be printed to the screen--
two pyramids-- one right aligned, one left aligned--
separated by two spaces in the middle with a height of 8.
And of course, if the user typed in a different number--
for example, if the user typed in a height of 3,
then you should display two pyramids with a height of 3,
for example, also with the two-space gap in between.
But if the user types an invalid input--
for example, they give you a negative number, or they give you zero,
or they give you a number that's too big--
then you should continue to reject that input
and re-prompt the user to type in a number
until the user gives you a number between 1 and 8 inclusive.
Once they do, then your program should display
the pyramids of the appropriate height.
So how are you going to do this?
The first step of your program is probably
going to be prompting the user for input, asking them
to type in how tall or short they want their pyramid to be.
In order to do this, you can use the GetInt function,
defined in the CS50 library.
The GetInt function is going to ask the user to type in an Int as input.
And it's automatically going to handle dealing with users that type in input
that are not integers.
So if the user types in a character or a word, for example,
GetInt will automatically take care of rejecting that input
and re-prompting the user to type in another number.
However, it's up to you to check to make sure
that the integer that the user does type in
is inside the valid range of 1 to 8 inclusive.
If the user types in a number that's outside of that range,
then it's up to you to re-prompt the user,
to ask them to type in another integer.
How might you do that?
Well, you might consider using a do while loop,
which is a loop that runs at least once and might continue to repeat
if some particular condition is true.
You might imagine inside the body of the do while loop,
prompting the user to type in a number, and then
having some condition that checks to see whether or not you should re-prompt
the user to type in another integer.
I leave it up to you to figure out what that condition should actually be.
Once the user has typed in some valid height between 1 and 8 inclusive,
the next step is going to be actually building that pyramid.
Ultimately, this is what a sample pyramid is going to look like.
In this case, we have a pyramid of height 7.
But I'll go ahead and replace the spaces with dots
so that we can visually see every character that you're going to be
responsible for printing to the screen.
Notice that on every row, we have some number
of dots representing the space before the left pyramid.
Then, we have some number of hashes representing the left pyramid itself.
Then, on every row, we have two spaces or two dots represented here
to separate the left half of the pyramid from the right half of the pyramid.
And then, we have some number of hashes representing the right pyramid.
You don't need to worry about adding spaces after the right pyramid,
because nobody will see those.
So what does that mean in terms of what you need to do?
Well, for each row, you'll want to print some number of spaces.
Then, print the appropriate number of left hashes.
Then, print a gap of two spaces.
And then, print all the hashes to make up the right pyramid.
But how do you know how many spaces or hashes you should print?
Well, you'll want to think about, given the height of the pyramid
and given what row number you're currently on,
how can you calculate the number of spaces that should be on that row,
how many hashes should be on the left side of the pyramid,
and how many hashes should be on the right side of the pyramid?
And for this, it might be helpful to draw some examples.
Draw a pyramid of height 3 or 4 or 8.
And see if you can come up with some sort of pattern or formula
that will let you calculate how many spaces and hashes you'll need.
After you implement that in code, the result
should be that you have a pyramid that looks something like this--
a left pyramid and a right pyramid separated by two spaces.
And you can test your code by first compiling it by running Make Mario,
and then running ./Mario, typing in some height between 1 and 8 inclusive,
and then seeing whether the resulting pyramid matches what you expect it
to look like.
My name is Brian.
And this was Mario.