1

I think my code can correctly print the pyramid,but it result errors in check50. What does that error in check50 means?

 Results for cs50/problems/2022/x/mario/less generated by check50 v3.3.7
:) mario.c exists 
:) mario.c compiles
:) rejects a height of -1
:) rejects a height of 0
:( handles a height of 1 correctly
    expected ""#"", not ""       #""
:( handles a height of 2 correctly
    expected "" #"\n"##"", not ""       #"\n" ..."
:) handles a height of 8 correctly
:( rejects a height of 9, and then accepts a height of 2
    expected "" #"\n"##"", not ""       #"\n" ..."
:) rejects a non-numeric height of "foo" 
:) rejects a non-numeric height of "" 

my code below

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

int main(void)
{
   int h;
   do
   {
      h = get_int("Height:");
   }
   while (h < 1  ||  h > 8);
   for (int i = 0; i < h; i++)
   {
         for(int n = 7; n > i ;n--)
         {
              printf(" ");
         }
         for (int j = 0; j <= i; j++)
         {
               printf("#");
         }
         printf("\n");
      }
}

1 Answer 1

1

The problem is the leading spaces on each line. This code will print 6 leading spaces on the first line no matter what. This is incorrect.

You could say that your code is simply printing the top h lines of a pyramid of height 8.

The program should print only the required number of spaces on each line so that the last line has no leading spaces. In other words, if the pyramid is height = 1, there should be one line and no leading spaces. Height=2 means only one leading space on the first line and none on the second.

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

3
  • Thank you very much for your answer and help! But the request in pset 1 asks for a right-aligned pyramid? I took a long time to make my program printing out 7 spaces on line one. Am I misunderstanding the task in pset1?
    – 123ewqeins
    Commented Dec 16, 2022 at 13:37
  • your program is printing 7 spaces in first row, no matter what height you enter. Try adjusting it. Commented Jan 7, 2023 at 7:49
  • Yes, you are misunderstanding the spec. The pyramid must be right aligned, but the bottom line must also start with no spaces. In other words, a pyramid of height 3 would have two leading spaces on the first line, one on the second, and none on the third. This code has 7, 6 and 5 spaces respectively. In other words, too many spaces.
    – Cliff B
    Commented Jan 7, 2023 at 8:06

You must log in to answer this question.

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