0

So I'm working on mario less and running into a road block. I got it to output the space and rows and columns but it's upside down, backwards and not the right number of spaces exactly. Like if you input 4 its:

###
#

The code is this:

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

//prompt user for  half- pyrimid height
// create blocks and right align them

int main(void)

{
    int height;
    do
 {
     height = get_int ("Input postive number between 1 and 23:  ");
     }
    while (height < 0 || height > 23);

    // add spaces and rows
    for (int i = 1; i <= height; i++)
    {
        height -=  i;
        printf (" ");

    // add hashtags and columns
        for (int n = 0; n < height; n++)

        printf ("#");
        printf ("\n");
    }


}
1
  • I changed for (int i = 1 to int i =0 and the spaces are right still upside down though.
    – MikaLand
    Sep 16, 2018 at 19:59

1 Answer 1

3

There's a couple of problems.

First, height is essentially a constant after it is entered by the user. It represents the number of rows to print, and as such, shouldn't be changed by the program. It should control the "main" loop, as you have done here.

Second, you want to print and increasing number of hashes on each row. Think about which variable is increasing.

Finally, you want to print height + 1 characters on each line. That means you need to vary the number of spaces that print on each line, which means spaces could (should?) be output in a loop too.

I have found this pset takes more thinking than coding. It might be helpful to get out the old pencil and paper. Draw the grid you want and evaluate how many spaces, how many hashes in each row. Then determine how each relates to height and which row is being processed (i in this case). That should give you the "math" you need to control the space loop and the hash loop.

You must log in to answer this question.

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