CS50 Mario-Less

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



int main(void)
{
    // Prompting user for how tall pyramid will be
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while(n < 1 || n > 8);
    // Building pyramid
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("#");
        }
        printf("\n");
    }
}

I have correctly controlled the user’s input so everything outside 1-8 won’t work and the user will be asked again. But I struggle to find a way to get a pyramid shape instead of a box. Can someone please help me out? Thanks

1 Like

If the user sets the height to 8, the n equals 8. So in your for loop above, how many #'s are always going to be printed out?

1 Like

For the loop above, 8 #'s will always be printed out. Giving me an 8 x 8 square grid. But if the user inputs 8, I need it to start at 1 # then followed by 2,3,4,5,6,7,8

The i loop is controlling the height and the j is the rows.

1 Like

Correct, so you need to adjust the for loop so that it only prints out the number of #s you need each time. You mentioned that you need it to start with 1 and work it’s way up to 8. Can you think of another variable you can use other than n that would allow you to do that? What is the outer for loop doing?

1 Like

The first for loop is controlling the height of the pyramid and the next for loop controls the rows. For example, if I removed the second loop and the user input 8, I would get 8 #'s stacked on top of each other. So knowing this, I shouldn’t use n in the second loop, but instead choose another character?

I don’t know, the problem doesn’t seem overly complicated, but for some reason my brain can’t think of a way to get the pyramid to always start with 1 # then essentially +1 # until it reaches n

1 Like

You are on the right track here. In the outer for loop, what is the value of i on the first iteration? The second iteration? The third iteration? Do you see a pattern here that you can use for the second for loop?

1 Like

I still feel very confused. The outer for loop is iterating +1 until it gets to the n amount the user inputed. But the user only controls the height of the pyramid, it is my job to make sure each row has the appropriate amount of #. Which means I need to create the value for the new character in the second loop?

Thanks for your patience, been stuck on this one the entire day, I promise I am trying

I think I need you to clarify what you should be printing. If the user enters 8, then should the first row print out 7 spaces and then a # for the eighth character? Or should it just print out one #?

I haven’t thought about having the second loop print out the necessary amount of spaces in each row. I was thinking I should print out all 8 on top of each other then the second loop would add 0 to the first row, 1 to the second row, 2, 3, 4, … and 7 to the last will make the base 8

Well, I’m looking at the assignment you linked to and it kind of looks like to me that it should be 7 spaces and then a #. And then the second row would be 6 spaces and two #'s, etc… That changes the inner for loop just a little. You do want it do loop until j < n. What you need to do is add logic inside the inner for loop so that it prints the correct number of spaces first and then the correct number of #'s.

That makes a lot of sense I just don’t know how to go about doing that.

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



int main(void)
{
    // Prompting user for how tall pyramid will be
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while(n < 1 || n > 8);
    // Building pyramid
    for (int i = 0; i < n; i++)
    {

        for (int j = 0; j < n; j++)
        {
            printf(" ");
            printf("#");
        }
        printf("\n");
    }
}

This just makes my 8 x 8 box of # more spaced out. How can I use spaces to delete # that aren’t needed? If that makes sense

I would think of it like this:

if (something) {
  printf(" ");
}
else {
  printf("#");
}

You need to figure out what something is. I will suggest that it might be easier to figure this out if you start your for loops at 1 instead of 0.

1 Like

Thank you! This is very close but the pyramid is facing the opposite way that it should be.

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



int main(void)
{
    // Prompting user for how tall pyramid will be
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while(n < 1 || n > 8);
    // Building pyramid
    for (int i = 1; i < n; i++)
    {
        for (int j = 1; j < n; j++)
        {
           if(j > i)
           {
            printf(" ");
           }
           else
           {
            printf("#");
           }
        }
        printf("\n");
    }
}

Your logic is just off a little.

if(j > i)
{
  printf(" ");
}

Is j ever going to be greater than i when the inner for loop first starts? No, so the # is always going to be printed before the spaces. You need to fix the condition in this if statement so that the opposite happens (the spaces are printed first).

Also, since you are starting the loops at 1 instead of 0, you might need to make another adjustment so that the correct number of rows/columns are printed out. Look at your sample output above. I’m assuming you entered ‘8’ but only 7 rows/columns are printed.

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



int main(void)
{
    // Prompting user for how tall pyramid will be
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1 || n > 8);
    // Building pyramid
    for (int i = 1; i < (n + 1); i++)
    {
        for (int j = 1; j < (n + 1); j++)
        {
           if (j <= (n - i))
           {
            printf(" ");
           }
           else
           {
            printf("#");
           }
        }
        printf("\n");
    }
}

Got it! Only took a full day :sob:

 if(j <= (n - i))

This is still confusing to me though.

Sorry one last thing, can you help me fully comprehend this line of code:
if(j <= (n - i))

I feel like I get but at the same time I don’t

I’m not sure what you don’t understand? So let’s just go through the first row in detail and see if that helps. If you entered 8 then n equals 8 and thus that line becomes:

if (j <= 8 - i)

Since both j and i start at 1, the very first time through the inner for loop, that statement will be:

if (1 <= 7) 

Which is true and a space will be printed. The second time through the inner loop that statement will be:

if (2 <=7)

And again a space will be printed. This will keep going until j gets to 8:

if (8 <= 7)

Which is false and thus a # will be printed.

Does that help?

1 Like

Wow thank you so much!! Makes perfect sense now. I was overcomplicating it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.