Like many others here I am completely new to coding. No experience whatsoever. I have been busy with the less comfortable Mario.c for a while now, trying to understand what is going on every step of the way. So far so good.

As I make my way toward a right-aligned pyramid, I have stumbled into a solution for a left-aligned pyramid by tinkering with the code cs50 provides as a "hint". I've been trying to figure out what the code is doing to make my left-aligned pyramid, but I can't seem to grasp it. Can anyone in layman's terms explain why this particular part of the code does what it does?

Note: I ask the user for an integer between 1 and 8, re-prompting when the number is above or below those numbers. After a valid number is entered, the number is stored and then passed through the following below (I think I am using those terms right!)

I believe it is this part of the program that tells the computer to draw the left-aligned hash pyramid. Why does it do what it does??

for (int i = 0; i < height; i++)

for (int j = 0; j <= i; j++)

1 Answer 1


Your analysis is correct, as far as it goes. The rest is pretty simple and is a basic demonstration of nested for loops. To make it clear, let's display the full loop correctly, since the curly braces are missing.

for (int i = 0; i < height; i++)
{    //start of outer loop code block

    for (int j = 0; j <= i; j++)
    {  //start inner loop code block
    }  //end inner loop code block


}    //end of outer loop code block

The first for loop, the i for loop controls the total number of times the code inside the outer set of curly braces will be processed. Practically speaking, it controls how many lines will be printed.

The second for loop, the j loop, controls how many times the code inside the inner set of curly braces will be processed, or how many times the # will be printed.

The process flow is this: The i loop setup line executes the first time, setting the loop up, checking the test condition and allowing the code inside the curly braces to execute if the test passes.

Next, the j loop does the same thing.

Now, the printf call prints one #. The code circles back to the j loop statement, the test is checked and the loop keeps executing until the test fails, thus printing the line of #s. When the test fails, the j loop skips to the first line after the inner curly brace and executes the last printf call, executing a line feed.

Now, the processing hits the curly brace for the outer loop, goes back to the i loop line, and continues the process in the same manner. The inner loop starts all over again, and so on, until the required number of lines are printed, or i = height. Finally, the code skips down to whatever code follows the loops.

Get your code to print the left justified pyramid correctly, then figure out how to add the spaces to make it right justified.

Hint: Use printable characters like x instead of spaces until it looks right, so you can see what's printing. Then change x to space.

Any questions?

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

  • Excellent explanation. Thank you very much!
    – Deadward
    May 23, 2019 at 2:48
  • You explained it really well, thank you. :) Jun 20, 2019 at 14:27

You must log in to answer this question.

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