0

I have submitted mario.c and while it printed the pyramid, it is printing 2 extra spaces in front of each hash row. What am I missing here?

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

int main(void) {
    //Declare variables
    int height;
    int spaces;
    int hashes;
    
    do {   
        //Ask user for height of pyramid
        height = get_int("Height: ");
    }

    //While user provides integer height between 1-8...
    while (height < 1 || height > 8);

    for (int i = 0; i < height; i++) { 
        //Print spaces
        for (spaces = (height - i); spaces >= 0; spaces--) {
            printf(" ");
        }
        //Print hashes
        for (hashes = 1; hashes <= (i + 1); hashes++) {
            printf("#");    
        }
        //Start on a new line
        printf("\n");
    }
}

Thanks

0

There are two errors in your program. Let's say for example that the height is 4. So in the loop, on the first run, spaces is equal to 4 and it can be >= 0. because of this, it prints 5 spaces in place of three spaces before the hash. So give spaces a value of height - i - 1 and make the condition to be spaces > 0.

Hope this helps. If it does then please check the tickmark.

You must log in to answer this question.

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