0

Having worked on Mario many hours now, I have code that shows no errors, but when I print it the hashtags each show up in their own row and not with others.

The basic premise in pseudo code:

asks for height --> has row # --> amount of spaces is height-2 --> amount of # is height-spaces

What's missing in my code?

int main (void)
{
    int height; 
    do
        {
        printf("How high should Mario climb?: ");
        height = GetInt();
        }
    while (height > 23 || height < 0); 

int i; 
    for (i = 0; i < height; i++) 
    {
int spaces;    
    for (spaces = height-2; spaces >=0; spaces--) 
    {
    printf(" "); 
int hash;
    for (hash = height-spaces; hash <= height; hash++)  
    {
    printf("#"); 
    printf("\n");
    }
    }
    }
    }

2 Answers 2

1

When you have each hash character on its own line, check the position of the printf for the special character '\n'.

It should only be printed when the line is finished printing the spaces and hash characters.

2
  • Thanks very helpful! However, now I fixed that, and everything is working correctly, except my pyramid is showing up backwards/reversed ie the base is on top and the top at bottom. I have checked the code and don't know what's wrong...can you please take a look? int row; for (row = 0; row < height; row++) { int spaces; for (spaces = height-2; spaces >= 0 ; spaces--) { printf(" "); } for (spaces = row+2; spaces <= height; spaces++) { printf("#"); //hash = height - spaces, goes up each time } printf("\n"); } Commented Dec 2, 2014 at 8:38
  • Check your for-loop for printing the spaces. How dose the count of spaces change, while the row increases?
    – Ewald Ertl
    Commented Dec 4, 2014 at 11:40
0

each row is a line right? and each line has both (unless it's height 0 or the last row) has first spaces, then sharps: That gives you when and where to change rows aka \n.

You can see that on each line , the sum of spaces and sharps is always the same on each row for the same height.

Now, you need to get that weird thing that is : It's not because the number of spaces goes down that you need to decrement in your for loop:

say you substract y to x. if x doesn't change and y goes up, you got a smaller and smaller number when you do x - y.

So:

  • readapt your sharps loop (your pyramid is upside down, with the big problem: you never have 2 sharps and that's what height one gives: 1 space 2 sharps)
  • your space loop never changes, and that usually means that your init and/or your end of loop condition are based on a variable that never changes.

You're almost there, go back to pen and paper, leave the computer for now, and take height 4 and draw a table where you write for each for loop the values of all your variables. I swear it helps!

Good luck!

You must log in to answer this question.

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