0

This is what I have for Mario.c less comfortable. I am having a problem printing the pyramid. Am I suppose to have a for loop for every space, #, and newline. for everyline. #=1 #enter image description here

1
  • This is what I have for Mario.c less comfortable. I am having a problem printing the pyramid. Am I suppose to have a for loop for every space, #, and newline. for everyline. #=1 #<height #++ (but for very first line, harsh gets printed twice) space= 0 space<height space--
    – Cnewbie
    May 16 '17 at 3:39
0

I used nested loops, two for loops within the outer for loop, like

    for (int i = 0; i < height; i++)
    {
        for (...)
        {
            putchar(' '); // print space
        }
        for (...)
        {
            putchar('#'); // print hash
        }
        putchar('\n'); // print line break
    }

or you could have a single for loop within, and inside use an if or the conditional operator ? : to print either space or hash.

Instead of putchar(' '), printf(" ") would probably do almost the same, that's just my personal preference. Use whatever you understand.

The inner loop bounds would have to be computed from i, or you'd keep an extra counter (I prefer calculation from i). In case of single inner for loop (which here has a number of iterations depending only on height) with if, that part would be in the if's condition.

I prefer always adding curly braces to control structures like if or for, as this reduces confusions.

You must log in to answer this question.

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