I have been working at this pset for hours. within 5 minutes i got the hello.c and water.c assignments completed. But when it comes to mario.c, I am utterly lost. I have watched every video. I watched the lecture twice. I watched the walk through, I have even searched through youtube and google as well as trying countless times to get this to work. Please someone fill me in on what I am lacking. I am having an insane amount of trouble getting this to work. I can't figure out for loops and everything i read about them frustrates me further. here is my code, maybe someone can help. I got the "#" to print the correct number of times, but the spacing still isn't doing anything. This makes no sense to me. A little help would be greatly appreciated.... here is my code

#include <cs50.h>
#include <stdio.h>

int main(void)
    int n;
        printf("Height: ");
        n = get_int();
    while (n < 0 || n > 23);
    for (int s = n - 1; s >= 23; s++)
        printf(" ");
    for (int i = 0; i < n; i++)
        for (int h = 0; h < i; h++)

I have just retyped the code so it would be easier to read with the post, so forgive me if there are any typos. Thank you for your help.

3 Answers 3


Using the 5 pyramid as a guide:

Height: 5

The psuedocode is straightforward:

for height times
   for s times
       print space
   for h times
      print hash

Of course, calculating s and h is the tricky part. They can both be calculated based on height. Break out the pencil and paper and make a grid:

 1 |      |
 2 |      | 
 3 |      |
 4 |      |
 5 |  0   |   6

The pattern should emerge, after that it's just arithmetic, basically the same arithmetic you will use to fill in the grid. (NB, I started the grid at the last line because counting spaces from the display is hard :).


Your first loop will never ever run. You have set the start index to n - 1 whose maximum value will be 22 since 23 is the maximum value for n and your condition is n >= 23 which will never be true for the value of i in the first place.

For the space part you have to print the space as well. You need to think of someway of inserting space using if condition. Let say the height is n then the width has to be one more than the height so you can say that if ( j < n - i - 1 ) print (" "); else print ("#") assuming i is your outer loop variable iterating from 0 to n - 1 and j is your innerloop variable iteration from 0 to n.


Assuming that the first loop was a typo and you actually intended it to read: s<=23, there is still the problem that your loop of spaces will run a set number of times. For example, if you are building a pyramid of height 5, your first loop would calculate s=4, then increasing s each time. In this case, as long as s<=23 your loop would run 20 times, when there are much fewer than 20 spaces in that version of the pyramid.

Thus a problem emerges with the static value of 23 being your end condition. You want the spaces printed to change based on the input of the pyramid's height. So you likewise want the end condition for your loop to in some way be relational to the height of the pyramid, number of hashes printed, or the row that it is on since those relationships all exist and change based on the pyramid you want to print. You've done this well in your loop for the hashes, making the end condition a function of two variables. Look to that to see how you might makes similar changes to the conditions of your first loop.

As Vikrant mentioned above, if the condition for your while loop is correct as written, the loop will never run.

You must log in to answer this question.

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