0

Problem at PSET1, Mario (More Comfortable)

I have written to output the pyramid as per specs. While they give the correct output when running the program, check50 is giving two errors with input of 2 and 23:

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

Below is the code I have written for your reference:

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

void pyramid(int);
void space(int);
void hash(int);

int main(void)
{
    printf("Height: ");
    int n = get_int();
    if (n < 0|| n > 23)
    {
        //rejects height value less than 0 and more than 23
        //repeats until valid input is provided
        while(n < 0|| n > 23)
        {
            printf("Please retry with value no greater than 23: ");
            n = get_int();
        }
        pyramid(n);
    }

    else
    {
        pyramid(n);
    }
}

//uses space() and hash() to print pyramid for each row
//i indicates the row # and n indicates the overall height
void pyramid(int n)
{
    for (int i=1; i < n+1; i++)
        {
            space(n-i);
            hash(i);
            space(2);
            hash(i);
            space(n-i);
            printf("\n");
        }
}

//prints required number of spaces according to int input y
void space(int y)
{
    for(int x = 0; x < y; x++)
    {
        printf(" ");
    }
}

//prints required number of hashes according to int input y
void hash(int y)
{
    for(int x = 0; x < y; x++)
    {
        printf("#");
    }
}

What is the reason behind this error message?Also, please give me formatting/styling suggestions to tidy up my code as I am not sure if that is readable for other people. External links and guidelines are welcome for this purpose too.

1

Remove the spaces at the right of the pyramid.

What you have:

__#_#__
_##_##_
###_###

What you should have:

__#_#
_##_##
###_###

Legend:

_: space
#: hash

Edit:

Your formatting is great, with one exception. The for loop inside pyramid() should have its body one tab to the left.

What you have:

for (int i=1; i < n+1; i++)
    {
        space(n-i);
        hash(i);
        space(2);
        hash(i);
        space(n-i);
        printf("\n");
    }

What you should have:

for (int i=1; i < n+1; i++)
{
    space(n-i);
    hash(i);
    space(2);
    hash(i);
    space(n-i);
    printf("\n");
}

Notice how the opening curly brace lines up with the for keyword.

2
  • 1
    Thanks! Was a mistake to visualize this in a grid form haha. – blankcheck Jul 21 '17 at 8:30
  • 1
    Also, thanks for the feedback on the formatting – blankcheck Jul 21 '17 at 8:36

You must log in to answer this question.

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