1

I ran the test and it was an error

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


int Height;
int space;
int hash;
int rows;

int main(void)

{

    do
    {

      printf("Welcome to Mario, Please pick Height from 0 to 23: \n");
      Height = get_int();

    }
    while ((Height < 0) || (Height > 23)); 

    // each row 
    for (rows = 1; rows < Height; rows++)

     {   
    for (space = (Height - rows); space > 0; space--)
         { 
             printf(" "); // print spaces
         }

    for (hash = 1; hash <= (rows + 1) ; hash++) // left hash
      {
        printf("#");
    }

        printf(" ");

    // right side code

    for (hash = 1; hash <= (rows + 1) ; hash++) // left hash
      {
        printf("#");
    }

        printf("\n");
    }
    return 0;

}

the error was

:( handles a height of 1 correctly
    expected "#  #\n", not " # #\n"
:( handles a height of 2 correctly
1
  • I spent quite some time figuring this out as well there should be 2 spaces instead of one and there should be no spaces after the second set of hashes Oct 8, 2020 at 1:44

2 Answers 2

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

The last row (or the first/only in this case) should be left aligned. As well, there should be 2 spaces between the half-pyramids. Can you see where yours is going wrong? You have space hash space hash instead of hash space space hash.

1

Much shorter alternative:

#include <stdio.h>

int main(void)
{
    int n = 6;
    char blocks[n];
    memset(blocks,'#',n);

    for(int i=0; i<n; ++i)
    {
        printf("%*.*s  %.*s\n", n, i+1, blocks, i+1, blocks );
    }
    return 0;
}

Result

Success #stdin #stdout 0s 9424KB
     #  #
    ##  ##
   ###  ###
  ####  ####
 #####  #####
######  ######

You must log in to answer this question.

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