0

I need your assistance with my code foe CS50 problem set 1, the mario(less comfortable).

I seem to have a few problems with the logic inside my code. Would you mind pointing out the mistakes? I would greatly appreciate if you could guide me through rather than showing me the answer.

Here is my code.

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

int main(void)
{
    int height;
    do
    {
        printf("Please type the height of pyramid: ");
        height = GetInt();
    }
    while (height < 0 || height > 23);

    for (int x = 1; x <= height; x++)
        {   
            for (int space = 1; space <= height - x; space++)
            {
                printf(" ");
            }

            for (int hash = 1; hash < height + 1 ; hash++)
            {
            printf("#");
        }
        printf("\n");        
    }
}

Thank you so much in advance for your help.

Ichi

2 Answers 2

1

You are printing too many hashes. Have a think about how many hashes you need to print relative to what line number you are on and use this information to create a limit for the for loop that prints the hashes.

1
  • I appreciate your advice. Thanks to you I found the a mistake in the logic in the third for-loop and detected the bug. Now the program runs correctly.
    – ichi
    Commented Feb 22, 2017 at 0:32
1

remember the hashes printed goes online with the value of x..so try connecting the condition of ur third for loop with the value of x...

1
  • I appreciate your advice. Thanks to you I found the a mistake in the logic in the third for-loop and detected the bug. Now the program runs correctly.
    – ichi
    Commented Feb 22, 2017 at 0:31

You must log in to answer this question.

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