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.