0

Hey guys so I've been struggling with how to go about getting my pyramid to print rightside up. So far I've only been able to get it to print with the base on the first row. Any help would be appreciated!

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

     int main (void)
  {
int height;
do
{
    printf("Height:");
    height = get_int();
}
while (height < 0 && height > 24);

 for (int rows = 0; rows < height; rows++)
 {
 for (int spaces = height - (rows + 2); spaces >= 0; spaces--)
 printf(" ");

 for (int hashes = rows + 1; hashes <= height + 1; hashes++)
 printf("#");

 printf("\n");
}
}

1 Answer 1

0

>24 should be >=24 or >23.

You print height-rows-1 spaces. And you print height-rows+1 hashes. rows is maybe a bit misnamed, as the variable specifies the current row, not all rows at once.

Your loops are a bit confusing. Two standard forms of loops are

for (int counter = 0; counter < repetitions; counter++)

and

for (int counter = repetitions; counter > 0; counter--)

In both cases, the number of iterations is easy to find.

Now to the loop with the hashes. That one should print rows + 2 hashes. 2 for the first row (where rows is 0), and one more on every row below.

You must log in to answer this question.

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