2

I have the first part of mario.c done. The computer asks the user for input and then continues to ask if the user does not input an acceptable number. However I am unsure of how to print the #'s and spaces.

I can print the number of #'s that the user inputs but I can not seem to make a pyramid, only a straight line of #'s. I have read some questions/answers here and on reddit and have read how this problem is really more of a problem solving problem more than it is a computer programming problem! So I have been stuck at the second half of mario.c for probably 2 weeks now and I am getting frustrated because I'm not sure how to go about attacking the second half.

I have written some stuff down on paper and some pseudo code but I just feel like I am not looking at the problem the way I need to be looking at it in order to solve it. Any help on how I should go about thinking when it comes to printing the pyramid would be helpful. I watched the short/walkthrough on Mario and it helped a little but I am still stuck on my loops.

Should I have a single for loop that the #'s and the spaces all happen in or multiple loops nested in one do ... while loop? I don't really understand how to make it so that there is the amount of "columns" that the user inputs.

2
  • Mario has been covered extensively here on stack exchange. Are none of the other answers helpful? If you could write out your pseudo code, or describe where you're stuck, we can point you in the right direction. Sep 5 '14 at 22:05
  • I am stuck on my loops. should I have a single for loop that the #'s and the space's all happen in or multiple loops nested in one do while loop? And I don't really understand or at least am having a hard time understanding how to basically make it so that there is the amount of "columns" that the user inputs. Sep 5 '14 at 22:22
7

I can not seem to make a pyramid, only a straight line of #'s.

In order to make a pyramid, you'll need more than one line. The way to move to the next line when printing out characters to the terminal/console is by printing a special character: \n, the newline character.

I am stuck on my loops. should I have a single for loop that the #'s and the space's all happen in or multiple loops nested in one do while loop? And I don't really understand or at least am having a hard time understanding how to basically make it so that there is the amount of "columns" that the user inputs

We know you will need multiple lines, so it's reasonable to expect you'll use a loop to go through and create each line, one by one.

Imagine you're inside this loop, and you're faced with a single line. You know how many lines there are total - the user told you the height before the loop started. Right now, this one line is all you care about.

You've already printed some lines, so you know which number you're on, be it the first, the fifth, or whichever. Thinking only of this one line, and keeping in mind that the user already told you the height of the pyramid, ask yourself:

  • How many hash # characters should this line have?
  • How many blank spaces should this line have?

Once you know the answers to those questions, you can print a space some number of times, print a hash # some number of times, and go to the next line. But the tricky part is that you need to answer in terms of:

  • Which line you're currently drawing (if we call it the "Nth" line, this number is N)
  • The total height of the pyramid (we can call this H).

It might help to list a few possibilities by hand. For example, take out a pen or pencil and a piece of paper, and write out a table like this:

+-----------------+--------+--------+
| Line number (N) | Spaces | Hashes |
+-----------------+--------+--------+
| 1               |        | 2      |
| 2               |        |        |
| 3               |        |        |
| ...             |        |        |
| H               | 0      |        |
+-----------------+--------+--------+

I started you out by filling in just two values; the specifications show that we need two hashes on the first line, and since the last line is the longest you won't need any spaces. Now here's a guide for you to solve the rest of this puzzle on your own:

  • First, fill in how many hashes you need for lines 2 and 3.
  • Then look at the relationship between the first and last columns, and fill in how many hashes you need for line H - remember, you will know what number H is when the program runs, but you don't know what it is beforehand! So you need to write your answer as a mathematical expression using H.
  • Finally, decide how many spaces you need on the other lines - but instead of just writing a number, see if you can find a way to express this number as a mathematical function of N and H that will be the same for every row.

Once you've solved that puzzle, write some code that prints out just the one line, if you tell it the total height H and the number of the line N. It needs to have the correct number of spaces to fit into the pyramid of height H. Once you've written that code, whatever it is, try to put it inside your loop for printing many lines.


I am now stuck on how to use the equation that I have come up with.... I am unsure where I need to put these equations(if they are correct) and I am confused on how to even implement N so that the computer understands it.

We already said that it's reasonable to expect you'll use a loop to "draw" each line on the screen, one by one. So your code that calculates how many spaces and hashes you need will go inside the body of that loop. The next question is, what kind of loop are you going to use?

You raise an important point: The computer needs to keep count of the lines as it draws them, in order to do calculations with N. This is a great reason to use a for loop, which has a loop control variable built in. The other big difference between for and while (including do...while) loops is that, with a for loop, you determine at the start how many times you're going to iterate. Here, the user inputs a height that determines how many times the loop needs to iterate, before the loop even starts; that's a clue that you should start by trying a for loop.

The basic syntax in C is:

for(initialization; condition; update)
{
    // loop body (one or more normal statements)
}

On the firstline you have the keyword for to start the loop, followed by the tripartite loop definition in parentheses. The first part is initialization, where you tell the loop what variable or variables will be used to control the behavior of the loop. Under older rules, you would have had to declare all your control variables before entering the loop:

int i;
for(i = 0; ...

In that example, the initialization step tells the loop that i will be used to control the loop, and gives it an initial value. Why i? No particular reason; you can call the control variable(s) whatever you want. Programmers are often in the habit of using letters like i, x or n to count numbers. The loop control variable can take any name that would be valid for any variable in C.

However, the appliance is set up to use the C99 standard, which allows declaring and initializing a variable at the same time in this first part of the for loop declaration:

for(int i = 0; ...

The second part, the condition, has only one purpose: Telling the loop when to stop. Usually it does this using one or more of the control variables from the initialization step. A typical example of a for loop that repeats 5 times might be:

for(int i = 0; i < 5; i++)
{
    // something that will be repeated 5 times
}

Maybe you're thinking, "Whoah! Slow down!" I went ahead one extra step, and wrote in the third part of the loop definition (the update statement) and the (empty) loop body. Why didn't I just show you the condition this time? Like this:

for(int i = 0; i < 5; ...

I have two answers for you. The first one is the easy answer: Most of the time, the update statement just increments the loop control variable by one. That's the most common usage and it's what beginning programmers expect a loop to do; we said that i would be counting the loop iterations, so it's got to count 0, 1, 2, 3, ... and so on.

The second one is the better answer, but not as simple: Without the update statement, there's no way to tell what the loop is going to do! The fact that we want the control variable to start at 0 and we'll stop when it's greater than or equal to 5 only tell us one thing: Since the initial value of the loop satisfies the condition, the body of the loop will be executed at least once. But we don't have to count one by one; you could write an update statement that causes the loop to only execute twice:

for(int i = 0; i < 5; i += 3)

Important to note here that the control variable is updated before the condition is checked on each iteration of the loop (except at the start of the the first iteration, when you're using the initial value(s)). So at the start of the second iteration, i is updated to 3, the condition is checked, 3 < 5 is true, and we proceed to execute the body. At the start of the third iteration, i is updated to 6, the condition is checked, 6 < 5 is false, and we leave the loop immediately. Here's a for loop that does nothing:

for(int i = 10; i < 5; i++)

Since the initial value of the control variable doesn't meet the condition, the loop terminates at the start of the first iteration, without ever running any code in the body. And here's an infinite for loop:

for(int i = 0; i < 5; i--)

There's no reason we have to increase our control variable; we can do whatever we want. In this case we're counting away from our condition, and the loop will keep going forever (or until the negative number becomes too large for the computer to handle, and the program crashes).

So that's the better answer to why, when I said here's a for loop that repeats 5 `times, I had to give you the entire loop definition and not just stop at the condition.

You might be wondering, how does this help me with mario.c? Well, remember we were talking about how to make the computer understand which line you're on so you can calculate the equations you came up with? The answer is: With the loop control variable. That is the missing ingredient. You will use the height variable that's holding the user-submitted value, along with the loop control variable that's going to start, end, and count the way you tell it to, to do that calculation.

As for what goes inside the loop body, I'm leaving it up to you. That is the smaller puzzle of "drawing" one line, when you know the values of H and N. You have all the tools you need; remember the idea of doing some small task a known number of times.

2
  • Okay that helped a lot but I am now stuck on how to use the equation that I have come up with. Which I have come up with are that: the amount of spaces is equal to H-N and the hashes are equal to H+1. However I am unsure where I need to put these equations(if they are correct) and I am confused on how to even implement N so that the computer understands it. Sep 6 '14 at 19:33
  • @Cory I had a lot to say in response to your comment, so I updated the answer with a second part. Hope it helps, and good luck with the problem set.
    – Air
    Sep 8 '14 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .