0

I solved the assignment but I do not understand this part:

for (hash = height - row -1 ; hash < height +1; hash++)

from

for (row = 0; row < height; row++)
    {
        for (space = height - row - 1; space > 0 ; space--)
        {
            printf(" ");
        }
        for (hash = height - row -1 ; hash < height +1; hash++)
        {
            printf("#");
        }
        printf("\n");

if height = 5

To me it looks like hash = 5 - 0 -1 = 4 but it show 2 hashes like it should be. Can someone explain it?

3

Let's trace your program.

if you take example height = 5 and you enter into your first for loop, the row = 0 and then you enter into second (first nested) for Loop.

space = 5-0-1 = 4

There will be one space printed on the Screen. Now space variable will be decremented by the Statement space-- and the new value of space will be

space = 3

There will be one more space printed and statement space-- will execute, remember you program is still in first nested for Loop. 4 spaces will be printed and your program will move to next for loop because the condition will be false when space < 0. now

hash = 5-0-1 = 4

one hash will be printed and hash = 5 because of hash++ and one more hash will be printed and again hash = 6 but this time for Loop will not execute beacuse the condition hash < height+1 is not true any more.

So on the first line you will have 4 " " and 2 #.

You must log in to answer this question.

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