简体   繁体   中英

CS50 Pset 1 Mario More

I'm solving Mario More comfortable in CS50 Pset 1, I did like most of it, and this is my code:

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

int main(void)
{
    int height, i2, i;
    do
    {
    height = get_int("Height: ");
    }
    while(height < 1 || height > 8);
    for(i = 0; i < height; i++)
    {
        printf("\n");
        for(int o = 0; o < height - i - 1; o++)
        {
            printf(" ");
        }
        for(int j = 0; j <= i; j++)
        {
            printf("#");
        }
        printf("  ");
        for(i2 = 0; i2 < height; i2++)
        {
            //printf("\n");
        for(int j2 = 0; j2 <= i2; j2++)
            {
            printf("#");

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

It draws the first pyramid well and puts two spaces, but instead of a pyramid, it draws something like a rectangle. I searched the internet for answers but all of them were just solving the whole thing and that spoils the learning process, so can you please give me some hints about this? I really appreciate any help you can provide.

If I am reading your question correctly, you want to produce a pyramid (or isosceles triangle) on the terminal like the following example (FYI, I have just one space).

Height: 8

       # #
      ## ##
     ### ###
    #### ####
   ##### #####
  ###### ######
 ####### #######
######## ########

If that is the case, I believe you complicated it a bit. In effect, you just want to repeat the pattern you have created in a symmetrical fashion. For that, you would only need to repeat the same print loop that you used to produce the left half of the pattern. If you need some more hints, let me know.

By the way, I believe that you can remove the "while" statement.

while(height < 1 || height > 8);

As it exists right now, if a value is entered that is less than one or greater than eight, that while loop will just become an endless loop.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM