0

I'm having trouble with mario.c. It works right for most inputs except 2 and 1 I think. Can somebody tell me what's wrong:

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

int main(void)
{
    int height;

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



    for (int rows = 0; rows < height; rows++)
    {
        int spaces = height - (rows + 1);
        int hashes = rows + 1;

        for (int j = 0; j < spaces; j++)
        {
            printf(" ");
        }

        for (int j = 0; j < hashes; j++)
        {
            printf("#");
        }
        printf("\n");
    }
}

When I use check50 here's what I get: :) 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 ""
1

I fooled around with it some and finally found the answer:

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

int main(void)
{
    int height;

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



    for (int rows = 0; rows < height; rows++)
    {
        int spaces = height - (rows + 1);
        int hashes = rows + 2;

        for (int j = 0; j < spaces; j++)
        {
            printf(" ");
        }

        for (int j = 0; j < hashes; j++)
        {
            printf("#");
        }
        printf("\n");
    }
}

YAY!!:)

You must log in to answer this question.

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