0

I think I got most of it already but I have one issue that I don't see how to solve. If I make the height 8 (for example) it'll print 1 empty row first and then 7 rows with hashes, the last row will have 7 hashes instead of the desired 8. Here's the code.

 for (int r = 0; r < i; r++) // makes a row
 {
        for (int s = i - 1; s > r; s--) // print left spaces
        { 
            printf(" "); 
        }
         for (int h = 0; h < r; h++) // print left hashes
         {
          printf("#");
         }
         for (int g = 0; g < 2; g++) // print gap
         {
             printf(" ");
         }
         for (int h = 0; h < r; h++) // print right hashes
         {
             printf("#");
         }
        printf("\n");

And this is what it makes

Height: 5

   #  #
  ##  ##
 ###  ###
####  ####

I forgot to mention "i" is the user's input for height.

How could I fix this? Thanks.

1 Answer 1

1

Look at the relationship between h and r. On the first row, r = 0. Then, in the inner for loops, h must be less than r, but starts at 0. In other words, h = r = 0 on the first row, so the for loop test fails and no hashes are printed.

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

2
  • Thank you! I just made the relation be <= and it worked. Is that the best way to go?
    – iKevin090
    Commented Jul 29, 2019 at 18:44
  • I'll let you study that on your own. One of the real skills needed by programmers is the ability to analyze code that already "works" and to figure out how to make it "work better". It comes with lots of practice! ;-)
    – Cliff B
    Commented Jul 29, 2019 at 19:05

You must log in to answer this question.

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