0

I am really struggling with mario.c from pset1. I am total novice to programming and so my question might be answered in some of the previous questions but I have not understood them.

My code gives me a pyramid that looks OK, and is OK for most of the rows, but when i do check 50, on rows 1 and 2, it looks like the pyramid should not be so close to the text of the prompt.

here is my code

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

   int main(void)
   {
   int height;

do 

{printf ("How high do you want mario to jump? Please choose a number     between 1 and 23 ");
 height=GetInt ();}

while (height < 0 || height > 23);

for  (int rows = 1; rows <= height; rows ++)

   {
       for (int spaces = 1; spaces <= (23-rows); spaces ++)

     {printf (" ");}

     for (int hashes = 1; hashes <= (rows+1); hashes ++)

     {printf ("#");}

     {printf ("\n");}
   }    

 }

and here is the error link: https://sandbox.cs50.net/checks/20242e1dd44f408da3ced0faf1f05a06

I know that there is something funny going on as all the hints I have looked at don't have their space conditions looking like this. I get the rows bit, I get the hashes bit, it is just the spaces. I don't understand how to possibly interpret the for condition when it involves having the spaces decrease with the spaces-- being used.

I have actually got it to work by modifying a loop I found in another of the answers,

    for (int row = 0; row <=height; row++)
    {     
    for (int spaces = height; spaces - 1 > row; spaces--)
    {
    printf(" ");
    }

   for (int hashes = 0; hashes < row + 2; hashes++)

but I don't want to use it - I want to know why mine is wrong and what the more conventional ways of going about it are actually saying. I know this starts at 0 whereas mine starts at 1.

Sorry this is so long and hope someone can help

Many thanks

1 Answer 1

0

Your test is failing because the pyramid is printing too far to the right. The code prints 24 characters on each row no matter what. The 23 high pyramid passes because that happens to be the correct layout for that pyramid. However, it is the only pyramid that is printing out correctly.

The correct pattern is that the bottom row of the pyramid prints with all hashes and no leading spaces. For example, a pyramid of height 2 has 1 space and 2 hashes in the first row and no spaces and 3 hashes in the second row. Your code prints 22 spaces and 2 hashes in the first row and 21 spaces and 3 hashes in the second row. You can't print any extra leading spaces or check50 will reject it. check50 looks for very exact output.

Take some time to work out on paper how to calculate the number of spaces. Start with a pyramid of height 2 and write down the number of spaces on each row. Repeat for heights 3, 4, etc. You should see a pattern emerge and be able to relate the number of spaces to the row number and the height.

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

3
  • Thanks for this I will check the code again. I now get that it doesn't print out 24 chars in each row and that the last row of each pyramid has no leading spaces, so i need to figure out how many leading spaces there are on the rows above the last....This is really tricky but I think I am overthinking it Commented Mar 16, 2016 at 19:03
  • Thanks again @CliffB. I have played about with the problem line and i think i am fundamentally confused about this. I can get a pyramid that is left aligned using for '(int spaces = 0; height-1 < rows ; spaces --)' but don't get how to specify the spaces to go backwards - so that there are no spaces on the bottom row. it works if i use (int spaces =height; spaces-1<rows; spaces --). Is it ok for int spaces to equal something that is variable? why do i have to change height in the condition to be spaces? sorry for the hassle and thanks for the help. Commented Mar 17, 2016 at 10:04
  • I should also say that I want to understand this rather than just getting the answer through trial and error. I feel thought that the code should have something to do with filling in columns, so that the columns are what the condition is based on, rather than the rows - does that make sense? maybe i am not cut out for coding - but paradoxically i have enjoyed this so far - i am just stuck! Commented Mar 17, 2016 at 10:07

You must log in to answer this question.

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