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

int main(void)
{
    printf("Please give men an int: ");
    int h = GetInt();

    char row;
    char column;
    char x;

    for ( row = 0; row < h; row++)
    {
        for (column = 0; column > h ; column--)
        {
            printf(" ");
        }
        for (x = 0; x < row; x++)
        {
            printf("x");
        }

        printf("\n");
    }
}

So this is what I've gotten so far, and it prints out a pyramid all right, but from the right.

I've used pen & paper a lot to draw out how many # and spaces there are on each line, but I'm struggling A lot with the equation and I'm beyond lost right now.

If anyone could give me some guidance towards the equation, I'd appreciate that so much, I know my problem is in the equation bit of it.

Thanks.

Edit:

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


int main(void)
{
    printf("Please give men an int: ");
    int h = GetInt();

    int row;
    int spaces;
    int x;

    for ( row = 0; row < h; row++)
    {

        for (x = 0; x < row; x++)
        {
            printf("x");
        }
        for (spaces = 0; spaces < h ; spaces++)
        {
            printf(" ");
        }
        printf("\n");
    }

}

EDIT 2

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


int main(void)
{
    printf("Please give men an int: ");
    int h = GetInt();

    int row;
    int spaces;
    int x;

    for ( row = 0; row < h; row++)
    {
        //Print spaces
        for (spaces = 0; spaces < row ; spaces++)
        {
            printf(" ");
        }
        //print Hashes
        for (x = -1; x < row; x++)
        {
            printf("#");
        }
        //New line
       printf("\n");
    }

}

It now prints

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

Why is this?

0

First, row column and x should be declared as ints and not as chars. Then, your first inner for loop, has the condition (column = 0; column > h ; column--) where h is the integer the user gave. Say that h is 5. On the first loop you assign column to 0, and you say "for as long column is greater than h do the following". But even on the first run it's false, as 0 is smaller than 5. So this for loop is never executed, and that's why you don't print any spaces. Think about it a little and if you can't figure it out leave a comment here. You should also change the step in your for loop too.

6
  • I just edited my comment with an update if you could. – JakobH Jul 24 '15 at 11:14
  • You should also print a new line after each row of hashes has been printed out. Do it by using printf("\n"); – ChrisG Jul 24 '15 at 11:26
  • oh, that was the comment formatting that messed up. They are stacked on top of each other. But they start <pre> # ## ### #### ##### </pre> – JakobH Jul 25 '15 at 8:01
  • You should first print the spaces and then the x's. Also you should use # instead of x. – ChrisG Jul 25 '15 at 8:08
  • Could you give my edit 2 a glimpse please. – JakobH Jul 25 '15 at 10:38
0

Okay I think I've got it now >.< Smalls steps is key I see :p (Haven't run it through Check50 yet though, it looks exactly as the finished pyramid of theirs. Thanks :)

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


int main(void)
{


    int h;
    int row;
    int spaces;
    int x;

    // Get input
    do {
          printf("Please give men an int: ");
          h = GetInt();
    } while (h < 0 || h > 23); {

    }

    for ( row = 0; row < h; row++)
    {
        //Print spaces
        for (spaces = 0; spaces < (h - row -1) ; spaces++)
        {
            printf(" ");
        }
        //print Hashes
        for (x = 0; x < (row + 2); x++)
        {
            printf("#");
        }
        //New line
        printf("\n");
    }

}

You must log in to answer this question.

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