0

I've finally gotten mario to behave how I want it to...except now it prints upside down. Here's my code.

int main(void)

{

//Grabs height from user between 1 and 23

 printf("Please enter a height between 1 and 23: ");

    int b = GetInt();


    //Return height and ends program if between 1 and 23
    if (b >= 1 && b <= 23) 
    {
        printf("Height = %i\n", b);   

        int x;
        int i;
        int c;       

        for(i = -1; i < b; i++)
        {
            for(c = i + 1; c <= x; c++)
            {
                char hash = '#';
                printf("%c", hash);
            }
            for(x = i - 9; x <= b; x++)
            {
                char space = ' ';
                printf("%c", space);
            }
            printf("\n");
        }
        return 0;
    }

//Closes program if not
 else
    {
     printf("Re-open program to try again.\n");
     return 0;

    }
}
1

I don't understand your logic for setting the limits of how many #s to print. First, you're printing a blank line. This is because you're using x as a limit, but x is 0 on the first pass. It's 0 because you don't set it before the first pass and it's using 0 by default or chance.

On the next pass, it's whatever is left in x from the next for loop, but from the previous pass.

The number of hashes is based on the row number alone. It doesn't matter how tall the pyramid is. It only matters that the first row has two hashes, the second row has 3, and so on. Why not just set the number of hashes based on i?

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

4
  • I set x = 0 and c = i in the second for loop, but all it output was:
    – K. Roe
    Sep 25 '15 at 3:35
  • Height = 5 ## ###### ##### #### ### I think I might have misinterpreted your answer
    – K. Roe
    Sep 25 '15 at 3:35
  • The each row of hashes was on a separate line, as was the 'Height = 5'
    – K. Roe
    Sep 25 '15 at 3:35
  • Try working out on paper what your program does. Work through each step and write down what happens with each variable until you understand what your code is doing.
    – Cliff B
    Sep 25 '15 at 3:41

You must log in to answer this question.

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