0

I just finished my mario pyramid, and tried myself, everything works. But in check50...

:) mario.c exists.
:) mario.c compiles.
:) rejects a height of -1
:) handles a height of 0 correctly
:( handles a height of 1 correctly expected "# #\n", not "# #\n"
:( handles a height of 2 correctly expected " # #\n## ##\n", not " # #\n## ##\n"
:( handles a height of 23 correctly expected " ...", not " ..."
:( rejects a height of 24, and then accepts a height of 2 expected " # #\n## ##\n", not " # #\n## ##\n"
:) rejects a non-numeric height of "foo" :) rejects a non-numeric height of ""

Which seems identical to what it expected...

Any idea if I missed anything?

Here is my code.

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

int main(void)
{
    int  height = get_int("Height: ");
    char brick = '#';
    char space = ' ';
    int  initial = 0;

    while (height < 0 || height > 23)
    {
        height = get_int("Height: ");
    };

    while (initial < height)
    {
        for (int i = initial; i < height - 1; i++)
        {
            printf("%c %c", space, space);
        };

        for (int i = 0; i < initial + 1; i++)
        {
            printf("%c", brick);

            if (i == initial)
            {
                printf("%c", space);

                for (int j = 0; j < initial + 1; j++)
                {
                    printf("%c", brick);
                };
            };
        };

        printf("\n");
        initial++;
    };
}

1 Answer 1

0

The pyramid that is printed out is skewed to the right because there are too many leading spaces on each line except the last. Your pyramid looks like this:

            # #
         ## ##
      ### ###
   #### ####
##### #####

It should look like this:

    # #
   ## ##
  ### ###
 #### ####
##### #####

I don't understand this line:

            printf("%c %c", space, space);

Stated simply, it prints 3 spaces. Why go through the convoluted method of creating a char variable to contain a space, and then a more complicated printf statement using the var, when you could print a single space like this:

            printf(" ");

and just execute it as many times as needed using a for loop?

Or, you could print 3 spaces like this: printf(" ");

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

1
  • thank you so much! turns out my pyramid is right, but the space between is wrong... you're right, maybe i shouldn't make it as variable :P Commented Jan 31, 2018 at 1:47

You must log in to answer this question.

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