0

So while approaching PSET1, my intent is to have a for loop that repeats itself as many times as the input height and then inside it two nested loops that will print #s and spaces.

Problem is, these nested loops don't seem to be working properly.

What I'm doing is this (in pseudo-codeish):

for (s = initial number of spaces; s == input height + 1; s--)
    {
     printf(" ");
     }

Then there's another loop for hashes using the same logic and then a line break printf.

Problem is, it doesn't work... maybe something wrong with the variables? Or maybe my whole logic is wrong?

Cheers!

1
  • Are you sure about == operator, it should rather be >=.
    – sinister
    Commented Feb 10, 2015 at 14:22

4 Answers 4

2

It might be that you set up the for loop incorrectly! You should have a condition in your for loop, like s > 0 or something along the lines.

1

Your psuedocode confuses me a bit. You start at the initial number of spaces, and while s is equal to the input height + 1, you decrease it? It sounds a bit odd to me.

You probably want to loop input_height times, and maybe print a space if it is below input_height-current_index and a hash if not? Play around with that for a while and see where it leads you?

1

I got it solved by declaring previously the initial number of spaces and the initial number of hashes to be printed (which is the core pattern of the problem) and then use those values in one nested loop for spaces and one nested loop for hashes. Then, in the main loop, I adjusted the values of those space and hash ints by - 1 and + 1 with every iteration.

0
0

The logic is correct. A for - loop with 2 more nested for-loops leads to a solution but have in mind that for the nested loops you must make use of the variable you used for the outer loop. Lets say you made a loop using a variable "lines" in order to draw the user requested height. That variable must be connected with the nested loops, so that the "space-printer" loop to have a math relation with the number of the line to be printed. Same for the hashes loop.

You must log in to answer this question.

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