1

Below is my code for mario.c which compiles and prints the required pyramid but for some reasons didnt pass check50.

// a program that recreates this half-pyramid using hashes (#) for blocks

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

int main()
{
    int N, space, hash;
    int i = 0;
    //prompts user for height of pyramid
    do
    {
        printf("Height: ");
        N = GetInt();
    }
    while(N > 23 || N < 0);

    //prints the half pyramid
    for(i = 0; i < N; i++)
    {
        for(space = 0; space < N - i; space++)
        {
            printf(" ");
        }
        for(hash = 0; hash <= i + 1; hash++)
        {
            printf("#");
        }
        printf("\n");
    }
}

And below is the check50 diagnostics;

~/workspace/pset1/ $ check50 2015.fall.pset1.mario mario.c
:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:( handles a height of 0 correctly
   \ expected an exit code of 0, not output of "\n"
:( handles a height of 1 correctly
   \ expected output, but not "\n ##\n"
:( handles a height of 2 correctly
   \ expected output, but not "\n  ##\n ###\n"
:( handles a height of 23 correctly
   \ expected output, but not "\n                       ##\n          ..."
:( rejects a height of 24, and then accepts a height of 2
   \ expected output, but not "\n  ##\n ###\n"
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""

can you please point out the reason for those errors.

1
  • Also main() should return some value. – Muhammad Ramazan Oct 31 '16 at 14:26
0

The logic of the code is such that it always prints a line feed for the first line of a pyramid of any height. Further, it also appears to be adding an extra leading blank to every line. check50 sees both of these problems as incorrect output. You need to rethink the formulas in the for loops to eliminate these issues.

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

0

:( handles a height of 1 correctly \ expected output, but not "\n ##\n"

If you analyze the pyramid output for n=1, then it should be ## but your output has an extra space before ##.

Similarly, for n=2, there should be single space in the 1st row and no space in the second row but your output has an extra space in both rows. The problem is in the implementation of space loop.

for(space = 0; space < N - i; space++)
       {
            printf(" ");
        }

you need to subtract 1 from the higher limit of space loop as space < N - i -1 to correct the error.

So correct loop would be.

for(space = 0; space < N - i -1; space++)
 {
  printf(" ");
 }

You must log in to answer this question.

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