0

I am having a hard time printing the #'s to build the mario pyramid. My input works great; my program uses a for loop to print the the appropriate number of rows. Nested in this loop is another for loop which provides the appropriate number of spaces for each row (currently printing as .). My second nested for loop consistently decreases the printed #'s for each row even though my for loop increments the control variable in the update statement. Here is the relevant code:

// print bricks

int spaces = height - 1;
for(int sharps = 0; spaces + sharps < h + 1; sharps++)
{
    string brick = "#";
    printf("%s", brick);
}

printf("\n");

Where is my logic error? I have studied every relevant mario question-and-answer and have learned a great deal but I've been going in circles with this for probably 15 working hours.

2
  • I answered a similar question here: cs50.stackexchange.com/a/2009/1707, the last part is relevant to your question. Commented Aug 22, 2014 at 22:10
  • Please could you provide additional information, on what h and height refer to. Assuming height is the value entered by the user, and h is the number of the row? Commented Aug 22, 2014 at 22:20

3 Answers 3

1

Why don't you set up two for loops in a do while loop:

int spaces = height - 1;  

do  
{      
    int x;  
    int y;  

    for (int spaces = x; x > 0; x = x - 1)  
    {     
        printf(" ");  
    }

    for (?????????)   
    {      
        printf("#");  
    }

    printf("\n");  
    ??? = ??? - 1;   
    ??? = ??? - 1;  
    ??? = ??? + 1;  
}
while(height > 0)

Just fill in the question marks and you should be good. Make sure you indicate how many sharps will ALWAYS be in the 1st row of the pyramid.

1
  • That was the ticket, Luke. Thanks for the recommendation - another 5 drafts and I got the program working correctly. Commented Aug 23, 2014 at 20:55
0

I don't see where you initialized h ?! :-/

You can make:

string brick = "#";
printf("%s", brick);               

into only one line, the first line is performing two instructions (declaration and initializing variable) in every iteration of the for loop.

0

Assuming height is the value entered by the user, and h is the number of the row, the value for spaces will be the same for every line.

int spaces = height - 1;

Note that this is a constant expression, which always evaluates to 1 less than height. So if height is 5 for example, then spaces will always have the value of 5 - 1 = 4.

What you probably want, is for spaces to relative to the row value h. There are many ways to calculate this, but suppose you did this instead:

int spaces = height - h;

Now the value of spaces will increase for each row. For example, again assuming height is 5, the value in the loop might look like this:

  1. spaces = 5 - 0 = 5
  2. spaces = 5 - 1 = 4
  3. spaces = 5 - 2 = 3
  4. spaces = 5 - 3 = 2
  5. spaces = 5 - 4 = 1
  6. spaces = 5 - 5 = 0

If spaces is used as the control variable in a loop which prints a single space character, this will print 5 spaces, then 4, then 3, and so on.


Now that you have the correct number of spaces, you can easily calculate the number of sharps in a similar way.

One way would be to just use the row number h as the loop control variable, e.g.:

for (int sharps = 0; sharps < (h + 2); sharps++)

You may notice that spaces is not included in the loop condition. The reason is that at this point the spaces should have already been printed, so all you need to do is print the additional sharp # characters.

If it's not clear why the loop uses sharps < (h + 2) and not just sharps < h, try both variations, and try different values instead of 2 to see what effect the value has.


A different way to calculate the number of sharps, would be to take into account the fact that the total number of characters for each row, including spaces and sharps, is always the same. So every row has the same number of characters.

If you know how many characters should appear on each row, and you already have the number of spaces, then the remaining characters must be sharps. The trick is calculating the correct number of characters (Hint: It's related to the height).

1
  • Very helpful solution and simple logic. Even helped me solve my own problem I've been experienced with my loops
    – user7324
    Commented Jun 9, 2015 at 20:15

You must log in to answer this question.

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