0

i wrote my mario.c programm and I am happy that it works. Well it actually prints a half pyramid. But it supposed to print the half pyramid from the left bottom corner up to the right upper corner. Well mine is the other way around. According to the walkthrough I am supposed to use spaces. Well the problem now happens to be that I have no clue where to put those spaces. Do I maybe use an extra for loop or include in in the excisting for loop. I tried various things but it never quite turns out the way I want it... Maybe someone has an idea? here my code:

include

include

int main(void)

{ int i,l,h; do { printf("state pyramid height: "); i = GetInt(); } while ( i <= 0 && i > 23 );

    for (l = 0; l <= i ; l++)
{

    for (h = 0; h <=(l+1); h++)
{
    printf(" #");
}
printf("\n");
}
return 0;

}

2 Answers 2

0

You're getting close here:

Do I maybe use an extra for loop

Think through a simplistic pseudocode of this process:

  1. Print correct number of spaces
  2. Print correct number of hashes
  3. Go to next line

You will repeat this entire process n number of times based on user input. You'll have to figure out the correct formulas that correlate line number to numbers of spaces/hashes. To do this successfully, you'll need one for loop for #1, one for #2, and one for the entire process. I'll leave it up to you to determine how they are nested and how to determine the proper formulas. I hope this nudges you in the right direction. As always, if all else fails, write out the logic by hand, then go back to implement it in code.

One other thing:

There's a logic problem in this line:

i <= 0 && i > 23 

Try some different test cases (negative, greater than 23, etc.) to see what the issue is.

0

I'm printing out the "spaces" and "hashes" by calling the printf command couple of times on different lines inside the loop.

The pattern for hashes (as mentioned in the walkthrough) is number of rows + 2.

And, the pattern for spaces is height - number of rows - 1.

You can put those into variables and work out the spaces and hashes as below,

    //print spaces
    printf("%.*s", spaces, "                         ");

    //print hashes
    printf("%.*s", hashes, "########################");

Let me know how you go.

Cheers

You must log in to answer this question.

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