1

Whenever I execute my code, I get a pyramid that is left-aligned instead of right-aligned. I think it is because my for loop for variable j is wrong and therefore none of the spaces will print. I tried to fix it but the pyramid is still left-aligned. My logic for the j for loop was that if per se 8 was the height picked. When substituted for i in i - 1 you get j equals 7 and 7 > or = to 0. Then j would decrease by 1 each time because j will always be greater than or equal to 0.

   for (r = 0; r < i; r++)
   {
     for (j = i-1; j >= 0 ; j--)
    {
      printf(" ");
    }
     for (n = 0; n < r+2; n++)
    {
      printf("#");
    }
    printf("\n");  
   }

Height: 5
 ##
 ###
 ####
 #####
 ######

1 Answer 1

0

You're almost there. Don't initialize your j to i-1, you can see this won't work when i=0.

Remember that the sum of spaces and #'s should total i+1. Work out with pen and paper first the equation relating the number of spaces and #'s on any given level r. So if i=8, the bottom level should have 9 #'s and 0 spaces, the one above that has 8 #'s and 1 space, and so on.

You must log in to answer this question.

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