1

I need help. can't seem to get the hashs to execute the way i want them to. Here is a sample of my code.

#include <cs50.h>
#include <stdio.h>
int GetPositiveInt(void);

int main(void)
{

    int height=0;

    if(height<23)
    {
        printf("Enter the height of the pyramid: ");
        height=GetInt();
    }

    while(height<0 || height>23)
    {

        height=GetPositiveInt();
    }

    int rowspace;
    int n=height;

   do
   {
      for(rowspace = 1; rowspace<height; rowspace++)
        {
            printf(" ");

        }
        for (int rowhash=0; rowhash<n; rowhash++)
        {
            printf("#");

        }
        height--;
         n--;


        printf("\n");


   } 
   while(height!=0);

return 0;

}

int GetPositiveInt(void)
{
    int height;
    do
    {
        printf("retry: ");
        height=GetInt();
        return height;
    }
    while(height<0 || height>23);
}
0

Let's consider a height of 5.

The pyramid should look like this

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

So for

height == 5 spaces == 4 hashes == 2

height == 4 spaces == 3 hashes == 3

height == 3 spaces == 2 hashes == 4

height == 2 spaces == 1 hashes == 5

height == 1 spaces == 0 hashes == 6

and that's where you should stop

What you print looks like this:

    #####
   ####
  ###
 ##
#
(extra new line)
height == 5 spaces == 4 hashes == 5

height == 4 spaces == 3 hashes == 4

height == 3 spaces == 2 hashes == 3

height == 2 spaces == 1 hashes == 2

height == 1 spaces == 0 hashes == 1

height == 0 spaces == -1 (prints nothing) hashes == 0 (prints nothing)

but this last line will print an extra new line, which shouldn't be there.

See if you can find the relation between the height and the hashes.

You can also check this and see if it helps even more.

You are pretty close. Happy coding! :)

1
  • 1
    Thanks bro ... that helped me understand the concept. Managed to get the pyramid facing the right way . Aug 23 '16 at 1:36

You must log in to answer this question.

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