0

I've been looking here on Stack for a solution to my problem and couldn't find one. My code seems to work properly, besides the spaces that looks like, are not beeing printed.

Here's the code:

int main (void) 
{
    do
    {
    printf ("Type input: ");
    height = GetInt();
    }while ((height < 0) || (height > 23));

    /*  First we see that the pyramid starts with a 2 hashes row.
        Then row = 1 and is <= to height. Incrementing till its the 
        size of the height   */
    for(row = 1; row <= height; row++)
    {
        /*The space aways follow the rule os -1 the input of the user. 
          and will be decreasing as the loop goes until reaches 0*/
        for (space = height - 1; space < 1; space--)
        {
            /*Prints a space*/
            printf (" ");``
        }

        /*The hash starts at 1 because it will be added with the amount of lines. Basically,
        this part here adds 1 to 1 making 2 hashes appear. and will be incremented till it 
        reaches the amount of lines desired. */
        for(hash = 1; hash <= (row + 1); hash++)
        {
            /* Prints out a '#'*/
            printf ("#");
        }

       /*Prints a new line, creating then, new rows based on the height input.*/
       printf ("\n");
    }
}

The output of the Hashes and Rows looks quite right. The problem is that it ends up beeing printed like this:

Remote debugging from host 127.0.0.1

Type input: 8                                                                                                                                     
##                                                                                                                                                
###                                                                                                                                               
####                                                                                                                                              
#####                                                                                                                                             
######                                                                                                                                            
#######                                                                                                                                           
########                                                                                                                                          
#########                                                                                                                                         

Child exited with status 0                                                                                                                        
GDBserver exiting

Am I missing something? I really couldn't find out whats going on...

1

First of all the space depends on row. So clearly it would be

for (space = height - row;

then as you are decreasing it will go down, you will continue until it hits 0, i.e., for positive only. So the final statement becomes

for (space = height - row; space >= 1; space--)

Happy Coding :)

3
  • It worked this way, but I would like to know more about the "spaces depends on row", What do you mean by this? Mar 23 '16 at 11:42
  • @MarcoA.Bomfim , because the row changes when the height stays constants throughout. If the spaces doesn't depend on the row, then you would be printing out the same amount of spaces (height-1) for every row.
    – Bobby
    Mar 25 '16 at 22:29
  • For the longest time I was using: space > 0 in the for loop thinking that it would allow me to get down to zero spaces. What I was finding was that it was adding an extra space in front of my hashes, and my pyramid wouldn't push up right next to the edge. Can you explain why I should be using space >=1 instead? Thanks Feb 15 '17 at 3:12
1

For starters, it won't compile because space isn't declared anywhere. But let's assume that it is declared as an int and just didn't get copied.

The problem lies here:

for (space = height - 1; space < 1; space--)

Either the loop will never execute or it will go into an infinite loop. Remember, the loop runs while the test passes. If space starts out greater than or equal to 1, the loop test fails before executing the first time. If it were to execute the first time, space is decremented on every pass, so it would run forever. Perhaps you meant >= instead of <?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Hi @Cliff-B, and thanks for the quick reply. The space variable actually has been declared as a global variable I forgot to put it in the question code. I tried the >= instead but the loop doesn't work properly yet. The Pyramid now starts with the input value in front of the hashes, but does not respond to the loop. Mar 22 '16 at 23:24
  • Actually, I think it's responding exactly as it is told. Maybe you should work out exactly what it will do -on paper- what it does for each row. Are you sure that you've figured out the correct relationships between the spaces and the values that control it? Hint: print something other than a space so you can actually see what it is doing, maybe an x.
    – Cliff B
    Mar 22 '16 at 23:36

You must log in to answer this question.

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