0

I have attempted the PSET 1 of 2020 CS50x and have a couple of doubts. Could someone check if the following code for Mario less one is correct. If it is I also had a question that what should I put instead of j > i and j <= i here and if my code is correct by chance what is the logic of this?

//blankspaces
      for (int j = n - 1; j > i; j--)
          printf(" ");
      //hashes 
      for (int j = 0; j <= i; j++)
          printf("#");
#include <cs50.h>
#include <stdio.h>
int main(void)
{
    int n;
    do
    {
        //take user input
        n = get_int("Height: ");
    } while (n < 1 || n>8);
    for (int i = 0; i < n; i++)
    {   
        //blankspaces
        for (int j = n - 1; j > i; j--)
            printf(" ");
        //hashes 
        for (int j = 0; j <= i; j++)
            printf("#");
        printf("\n");
    }
}
0

Your code runs correctly. For every row, you need to print n-i-1 spaces. Your first for loop accomplishes that goal. Then, you need i+1 hashtags for every row which you do by incrementing from j=0 to j <= i. That is the logic for your two loops.

| improve this answer | |

You must log in to answer this question.

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