0
4 int main(void)
5 {
6    int height;
7    int rows;
8    int spaces; 
9    int hash;
10
11   do
12   {
13   printf("For the height, Choose a number from 0 to 23?: ");
14   height = GetInt();
15   }
16   while ((height < 0) || (height > 23));
17   
18
19   for (rows = 1; rows <= height; rows++)
20   {
21       for (spaces = rows + 1; spaces <= height; spaces++)
22       {
23            printf(" ");
24       }
25       for (hash = spaces + 1; hash >=height; hash--)
26       {
27           printf("#");
28       }
29       printf("\n");
30   }
31   return 0;
32   }

Alright guys, so I'm running into some issues with my code. When I input the height such as 7, this is what I get:

enter image description here

I'm trying to make the pyramid out of hashes that aligns to the right of the spaces and is equivalent to the inputted height. As you can see here, I'm having difficulty forming the pyramid of hashes. However, the spaces seem to be working. I know the issue is right in front of me, but as of now, I can't figure it out. Can someone see where the flaw is in my code as to why the hashes will not make the desired pyramid?

1 Answer 1

0

There's an old saying, "KISS - keep it simple, stupid!", something I seem to tell myself on every project I work on. ;-) Look at the statement that controls printing of the hashes:

25       for (hash = spaces + 1; hash >=height; hash--)

This will always print 3 hashes. spaces will always leave the previous loop as height + 1 so this loop will always run 3 times.

You have an idea what to do, but maybe haven't zeroed in on it yet. The number of hashes isn't related to the number of spaces, it's directly related to the row number that's currently being printed. Think about it for a minute. How many hashes should be printed on the first row of a pyramid? The second row? Does the height of the pyramid change these numbers? Does the number of spaces have any bearing on how many hashes to print? Remember, KISS! ;-)

It's interesting, most people get the hashes right and have trouble with the spaces! You got the spaces right and are having trouble with the hashes! ;-) Other than this problem, your code is tight and clean. Good work!

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

1
  • Oh wow, I think my brain was just overwhelmed. Thank you for clearing this up!
    – vslateart
    Commented Sep 16, 2015 at 18:03

You must log in to answer this question.

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