1

I think I have a problem with the syntax of my main loop that counts the number of lines to output. When I set the counter = 1, it prints the correct number of rows but starts at the top with only 1 hash. And according to the directions, the top row of the pyramid should be 2.

So, then when I increase my main loop counter to 2, it starts the top row correctly as 2, but then wont print the final bottom row. So, for example if I enter 8, it gives me 7 rows, starting with the two hashes on top.

So, I think it just needs to go through one more counting loop, in which it repeats the embedded hash and space loops one last time to print my final bottom row, but I cant figure out what to adjust. Here is my code for the counting loop:

for(counter = 1; counter <= height; counter++)

so, to recap, if counter = 1, I get right number of rows but top row is only 1 hash. If I adjust counter = 2, I get 2 top hashes but short one row.

2 Answers 2

1

First of all, notice that inside a for loop you can use other variables as well, not only counter. E.g.:

for(counter = 1; counter <= height; counter++)
{
    int row_number = counter;
    int hash_quant = counter + 1;
    ...
}

Even further, you don't need to store the quantity counter + 1 in an extra variable. You could simply use it as is, in any expression where you'd write copy:

for (hash_counter = 1; hash_counter <= (counter + 1); hash_counter++)
{
    printf("#");
}

The main idea behind this pset is that you need to perform two different counts:

  • one that counts the number of rows to be printed out
  • another one that counts the number of # to be printed out in each row

Of course, those two counts are related, and the challenge in this pset is to find (and code) such relationship.

0

If you are getting only one # that mean the loop is executing only once. Check that. And just a hint you need 3 for loops for this.One for the space and other two for printing # and the new line character. And the syntax of you for loop is correct. HINT: Try nesting two loops (separately) in a FOR loop.

You must log in to answer this question.

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