-1
#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int height, row, spaces, hashes; //declare all variables to be used    
    do //prompt the user for valid height inputs that are non-negative and less than 23.
    {
        printf("Height:");
        height = get_int();
    } while ((height < 0) || (height > 23));

    for (row = 1; row <= height; row ++) // iterate through each row of the height
    {
       for (spaces = (height - row); spaces >= 0; spaces--) //print the number of spaces for the ith row
       {
           printf(" ");
       }
       for (hashes = (row + 1); hashes <= (height + 1); hashes++) //print the number of hashes for each row
       {
           printf("#");
       }
       printf("\n");
    }
}

Not sure what I am missing in the above code. The spaces print as expected, but the behavior of the hashes is inverted, printing the highest and decreasing.

  • 1
    Possible duplicate of [Creating a "Mario Style Pyramid"](http://stackoverflow.com/questions/38961481/creating-a-mario-style-pyramid) – Imanuel Apr 16 '17 at 17:07
  • I hope that you'll get it after reading [this answer](https://stackoverflow.com/a/38961720/6290553). – Raktim Biswas Aug 08 '18 at 09:59

1 Answers1

1

Let's assume the user submitted 12 as height.
You start the outer loop with row = 1, which gives spaces from height - row = 11 to 0, makes 12 spaces.
The hashes go from row + 1 = 2 to height + 1 = 13, makes 12 hashes.
Next iteration of the loop: 11 spaces, 11 hashes.

What you have expected can be achieved if you changes the second inner loop:

for (hashes = 0; hashes < row; hashes++) //print the number of hashes for each row

If this is not exactly what you need, change beginning of the loop to hashes = 1; or the end to row - 1; or whatever you need.

Your mistake was to assume that hashes++ makes it ascending. It does, technically, but it only changes the order of the loop, not how often it is run. Since you do not need the actual value of hashes, the order is irrelevant. The only thing that counts is how often the loop is run ;)

Imanuel
  • 3,596
  • 4
  • 24
  • 46