0

I am completely stuck with the hashes for this problem set. I've read thru a bunch of different forums and questions already posted, but i honestly have no idea what to do. Every time i try to change something, i get stuck in an infinite loop.

Here's my code: *note: i'm using "." instead of spaces for now so i can see whether is it's correct.

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


int main(void)
{
    //Initialize necessary variables
    int height, row, space, hash;

        //Get user's desired height
        do
        {
          printf("Please enter a height between 1 and 23. \n");
          height = GetInt();
        }
        while (height <= 0 || height >= 23);


    //print # of lines/rows
    for (row=0; row<=height; row++)
    {

         //print spaces
         for (space=height-(row+1); space==0; space--)
         {
             printf(".");
         }

        //print hashes
        for (hash=row+2; hash<=height; hash++)
        {
            printf("#");
        }
        printf("\n");


    }

}

I'd like to avoid receiving straight up solutions, just a point in the right direction. Thanks in advance!

1

You are basing the number of hashes on a relationship between the row number and the total number of rows. This is the wrong approach.

Think about how many hashes should be printed on each line. For any height of pyramid, the first line is always two hashes, the second line is always 3, etc. More importantly, the height of the pyramid makes absolutely no difference to the number of hashes being printed on a given line.

That should get you going. Next, you'll need to figure out the relatiionship between the row number and the height to print the spaces. Get the hashes worked out first though.

BTW, 0 is an acceptable input for the program.

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

You must log in to answer this question.

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