0

I am currently working on pset1 Mario, less comfortable. I am running my c to turn the hashtags into a staircase, but when I run the code, it is only a block. What is wrong with my code? Thank you all for helping me out and have a fantastic day.

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

// Function that makes the pyramid
int main(void)
{
// Declares the variable
int n = -100;

// Do loop gets the height of the pyramid
do
{
    n = get_int("How many blocks tall do you want your pyramid to be? ");
}

// Ensures that the number for the pyramid isn't less than 0 or greater than 23
while (n < 0 || n >= 23);

// Rows
for (int i = n; i > 0; i) {
    printf("#");
    // Columns
    for (int j = 0; j < n; j++) {
        printf("#");
    }
    printf("\n");
}

}

0

That's because you're not changing the number of '#' that will be printed at each row. I won't give the actual code to do so but try having an inner for loop whose number of iterations change with different iterations of the outer loop.

5
  • So instead of having the j for loop rely on n, have it rely on i? Jul 22 '18 at 19:26
  • @Luke Exactly. Although it may not do the job completely but it's a step in the right direction. You should remove the printf function of the outer loop since it's only adding an extra hash on every iteration, try to print things only using the inner loop(s) using arithmetic on variables in the for loop condition to get the desired number of iterations.
    – kappa122
    Jul 22 '18 at 21:00
  • Thank you so much! That worked perfectly Jul 22 '18 at 21:38
  • The pyramid is now left-aligned instead of right-aligned. Do you know what could be wrong with my code? Jul 22 '18 at 22:05
  • @Luke You need to print out some spaces before the hashes to make it right-aligned. For that you'll need another inner for loop, so there's one for spaces and one for hashes, the number of spaces should decrease after each iteration and the number of hashes should increase.
    – kappa122
    Jul 23 '18 at 5:58
0

Actually, are you sure that you got an output for this code, without any errors? Actually, there are some bugs in your code. 1. You initialized a variable without any purpose n = -100 is like, u don't need it, because you are prompting the user for that and the user will give a value.

  1. To show that, "the height should be between 1 to 23" you must use the boolean AND(&&).

  2. The syntax of for loop is, first to initialize, then compare, then go on to the code block and then INCREMENT/DECREMENT. you haven't done that here

  3. And also please reconsider the logic you have used in the for loop.

You must log in to answer this question.

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