0

So, I am trying now to make a pyramid of hashtags which in every row has a number of spaces before from the left side of the hashtag. In the program it starts with a prompt from the user between 1-8 , and then that´s the height of the pyramid. Like this:

      #
     ##
    ###
   ####
  #####
 ######
####### 

To start with, as a first step, I am putting dots (.) where the spaces should go, to make it visible to me. And then comes the hashtag. I am wondering why it only gets printed dots when I programmed like this?

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

int main(void)
{
    int height, rows, width, space;
    
    //Get prompt, number of rows from user
    do
    {
        height = get_int("How many rows? ");
    }
    while(height < 1 || height > 8);
    
    //Number of rows
    for(rows = 0; rows < height; rows++)
    {
        //For number of dots (width)
        for(space = 1; space < (height - rows); space++)
        {
            printf(".");
        }
        //For number of hashtags
        for(width = 0; width > (height - space); width++)
      {
        printf("#");
    }
    printf("\n");
    }
    
}

Actually now I think I solved it! But please help me to formulate the logic. Because I am limping in my thought to explain it clearly in my head... Anyway I solved it like this:

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

int main(void)
{
    int height, rows, width, space;
    
    //Get prompt, number of rows from user
    do
    {
        height = get_int("How many rows? ");
    }
    while(height < 1 || height > 8);
    
    //Number of rows
    for(rows = 0; rows < height; rows++)
    {
        //For number of dots (width)
        for(space = 1; space < (height - rows); space++)
        {
            printf(".");
        }
        //For number of hashtags
        for(width = (height - rows); width <(height + 1); width++)
      {
        printf("#");
    }
    printf("\n");
    }
    
}
1
  • I tried to set the width in the for loop on 1 as well but then it becomes an infinite loop. Apr 1 at 12:47
0

The original problem lies in the for loop setup:

    for(width = 0; width > (height - space); width++)

Look carefully at the test condition. It says to run the loop only if the value of width is LARGER than height - space. Then, the increment clause causes width to get larger with each pass.

With this, one of two things will happen. Either the loop will never start because width is too small, or if it does start, it will be an infinite loop because width increases (it never decreases). It should have been a less than, not a greater than in the test clause. I believe you've said you demonstrated both events. ;-)

Remember, the test condition must be TRUE for the loop to execute. A common newbie error is thinking that the loop ENDS when the test condition is true. It only ends when the test condition is false. (Or it doesn't start.)

Any questions? ;-)

BTW, while changing > to < will fix the operation of the loop, I make no statement whether the correct number of hashes will print. Why don't you try it and see what happens? ;-)

1
  • Thank you for explaining so clearly! Appreciate a lot! @Cliff B Apr 7 at 8:57

You must log in to answer this question.

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