So I managed to finish pset1 mario.less but I'm really struggling to understand the logic behind my own code, the confusion comes into play once I get to the part where we begin building the half pyramid. Could someone please explain how the pyramid is being built in plain english for me? I'm specifically confused when it comes to the nested for loops.

int main(void) { int height; int row; int spaces; int hash;

// do-while loop which prompts for height greater than 0 and less than 23

do {
    printf("height: ");
    height  = get_int();
while (height < 0 || height > 23);

// drawing the pyramid

// while the amount of rows is less than the height
for (row = 1; row < height; row++){

    for (spaces = height - row; spaces > 1; spaces--) {

        printf(" ");

    for (hash = 0; hash <= row; hash++ ){




1 Answer 1


You have an outer loop. It runs height-1 times (shouldn't it be height?). The loop variable goes from 1 to height-1.

Inside this outer loop's body, you have two loops and a print newline. The first inner loop runs height-row-1 times and prints spaces, the second runs row+1 times and prints hashes. Together, this makes height characters per line (should be height+1?)

If the loop variable does not matter (as in this case), consider using one of the standard forms

for (int loop_var = 0; loop_var < num_iterations; loop_var++)


for (int loop_var = num_iterations; loop_var > 0; loop_var--)

(variable names are arbitrary, I want to emphasize the 0 and either < or >) which both clearly show the desired number of iterations.

You must log in to answer this question.

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