0

So i have managed to get the first pyramid to print how I wanted to. However I have no idea where to start with writing the second pyramid. There does not seem to be anything online creating the second pyramid with two spaces - have i missed something?

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

int main(void)
{
    int height;
    do
    {
        height = get_int("What are the pyramid heights:");
    }

    while(height <= 0  || height > 23 );
        int spaces = height -1;
        int hashes = height - spaces;
        for (int i = 0 ; i < height ; i++) {


            for( int j = 0 ; j < spaces  ; j ++) {
                printf(" ");

            }

            for( int k = 0 ; k < hashes  ; k ++) {
                printf("#");
            }
              printf("\n");
              (spaces --);
              (hashes ++);

        }

}
0
  1. 0 is a valid input value. It means zero lines, so no output (your for loop already handles this case correctly, so only place to change is the do..while condition).
  2. Indentation is off. Pretty confusing since you add a level of indentation just after the while, as if it were a while and not do..while loop.
  3. (finally) You could just repeat your hash-printing loop, and print two spaces (without any extra loop) between those loops. Note that there are no trailing spaces after the second half-pyramid, the lines end with a hash.

You must log in to answer this question.

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