>> ZAMYLA CHAN: Let's jump into Mario.
Mario is a program where we'll make our own, albeit primitive, version of
a classic Super Mario Brothers game background.
For this problem set, we're going to recreate the half-pyramid for
Mario to jump on.
Our program will be interactive, so it will ask the user to input a certain
height for the pyramid.
And the program will print out a half-pyramid of that height, where the
bottom left hash aligns with the bottom left corner of the terminal
output window.
>> Let's break this problem into two parts, one, get user input, and two,
print out the pyramid.
Remember that though the GetInt function will retrieve an integer, you
have to make sure that this input makes sense for your program, so in
Mario, it won't do just a product for an integer.
You'll need to make sure that the user inputs an integer that's within the
lower and upper bounds of the pyramid that you can print.
Per this spec, this is 0 and 23 inclusive.
If the user inputs an integer outside of our accepted bounds, then we want
to prompt them again and again until they give us a valid integer.
>> One way to ensure correct user input is by using a Do-While loop, which is
very similar to a While loop.
The Do-While loop executes the code inside the body once, and then checks
whether the condition is met or not.
This is useful for obtaining user input because you know that you need
to prompt them at least once.
If the condition is not met, the program will execute the line after
your Do-While loop.
If the condition is met, though, the loop will repeat.
>> A Do-While loop for validating user input will look something like this.
I declare a variable n, GetInt, and repeat until n is valid.
Remember that when you declare your variable, it needs the appropriate
scope, just like in Scratch.
If I declare n inside my Do-While loop, the rest of the program won't be
able to access it.
It's limited to the confines of the curly braces.
>> All right, so now that we've validated the user's input, we actually need to
draw this half-pyramid.
It's made up of printed characters, so let's make a half-pyramid in a simple
text editor.
If we want to make a Mario style pyramid of height three that's aligned
on the left side of our window, then we'd type two hashes, click Enter,
then type three hashes, click Enter, and then type four.
>> But in this problem set, our half-pyramid has to be right aligned.
Using a standard keyboard, how might you modify this file to move hashes
over to the right side?
I might use to underscores, two on the top row and one on the second.
That doesn't look as nice, though, so let's replace the underscores with
spaces, and there we have a half-pyramid of height three.
>> Try and remember this text editor example as you start thinking back to
C. Let's try and figure out some kind of pattern that we can
put in a loop construct.
Take an example height, say eight, and see if you can start to
come up with a pattern.
The first row will have seven spaces followed by two hashes.
The second row will have six spaces and three hashes, and so on until the
eighth row.
>> But how do you would represent the nth row?
Remember that programming convention is zero indexed.
That means that you start counting at zero, so the first row is technically
row number zero with two hatches, row number one has three hashes, row
number two has four hashes, so following this pattern for any row
number n, there are n plus 2 hashes.
I'll leave it to you, though, to find out the pattern for the spaces.
Remember that your pattern will be slightly different if you choose to
start your count from one and not zero.
>> Now, you have a pattern for any abstract row n.
You know how many spaces to print and how many hashes, so for each row, that
number will change.
But the same process of printing a single character, either a space or a
hash, is repeated.
So all you have to do is repeatedly print that character for as many times
as your pattern determines.
>> So how do we repeat processes?
In Scratch, we used the repeat block.
In C, we'll use a For loop.
Let's look at the syntax for For loops.
>> Each For loop is composed of three parts, an initialization, a condition,
and an update.
Your For loop will initialize a variable when it first
enters the For loop.
If the condition is met, the body of the loop will execute.
Afterwards, the update will execute.
>> If the condition is still met, the loop will execute and update and
repeat as long as your condition evaluates to True.
You're condition must eventually valuate to False, though, because
unlike Scratch, we don't have any Forever loops.
Your program must end eventually.
>> Here's an example of a For loop that you might want to use for Mario.
The initialization declares an integer i with value 0.
As long as i is less than the height, the body of the loop will execute and
increase i by one and repeat until i is greater than or
equal to the height.
>> Now, my For loop is zero index.
The Int i starts at zero, not one.
If I chose one, then my condition would also have to be different for
the loop to execute the same number of times.
Remember this carefully when you're choosing your initialization and your
condition, and consult this chart to double check.
>> One of the great things about computer science is that there's just so many
ways to execute things.
You can choose whether you start at zero or one.
If you don't want to use a For loop, you can also use a While loop to
repeat statements as long as you keep the essence of a For loop,
initialization, condition, and update.
Once we figure out the For loop that we're most comfortable with, then we
can complete Mario.
>> We first checked for user input and then identified a
pattern for any row n.
So for each row from zero to n minus 1 will print the appropriate number of
spaces, then the appropriate number of hashes according to our pattern, and
then a new line.
With that, you have your pyramid.
My name is Zamyla, and this was Mario.