0

I have been writing this code for a day but I still could not get the result I want, I don't even know how the is giving me a weird result.

Here's my code:

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

int main(void)
{
    int n;
    {
    n = get_int("Height: ");
    }

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


   for (int i = 0; i <= n; i++)
  {
      for (int j = 0; j <= i ; j++)
      {
          printf(".");
      for (int k = 0; k <= j  ; k++)
      {
          printf("#");
      }
      }
      printf("\n");
  }
}

I have 2 questions with my code:

  1. I am not sure which line of this code is going to give me a pyramid shape? What is the logic behind for generating the pyramid?

  2. This code generated a weird pattern which the dot and hash are mixing up, how should I separate them?

Thank you in advance.

(English is not my first language and this is my first time coding, sorry if I am asking silly question)

1

You don't have the indentation right in the for-loop section of your code, which I think is making it harder for you to see what's wrong. Here's that section, with proper indentation.

for (int i = 0; i <= n; i++)
{
    for (int j = 0; j <= i ; j++)
    {
        printf(".");
        for (int k = 0; k <= j  ; k++)
        {
            printf("#");
        }
    }
    printf("\n");
}

Answer to question 2: So in the j loop in your code, you're printing one ".", then going into the k loop to print some "#", then going back to the j loop to print another one ".", then going into the k loop to print more "#", and so on until you're done with the j loop, and print the newline in the i loop.

"What is the logic behind for generating the pyramid?" That's what you're going to have to figure out. It looks like you're working on the "Right-Aligning with Dots" step of the problem. Does your code from the "Building the Opposite" step work correctly? If it does, leave the section of your code where you print the "#" alone, and add another section where you print periods right before it. If you don't have the "Building the Opposite" step working, go back to the first step you do have working.

Something that's a good idea to practice is doing the steps of your code yourself. You would take a piece of paper, and go through each line of your code, keeping track of what the variables are, and doing what your code says. This helps understand how things like loops work, and makes writing code easier in the future.

A couple other comments on your code:

It looks like you're going to have "off by one" errors. If n = 4, you should be printing exactly 4 lines, but for (int i = 0; i <= n; i++) this line of code will have you printing 5 lines. Generally, loops like that are written with the middle being i < n (no equals sign). You should go through the loop yourself to see how that works.

Also, it doesn't look like the section where you get the user input will work right with bad input. Try looking at the notes on the website to see how a do-while loop should look.

| improve this answer | |

You must log in to answer this question.

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