2

I'm very new to programming and I'm trying to complete the cs50 course without just copying other people's code and trying to understand why mine won't work. Currently working on the first C assignment to create a pyramid of #s but, when I run this code, I run into some issues. It compiles fine but when I run it nothing seems to happen.

Can someone point out why this won't work and/or how to fix this?

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

int main(void)
{
int spaces;
int bricks;
   int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1);
    {

    for ( int i = 0; i < n; i++)
{
      for (spaces = (i - 1); spaces >= 0; spaces++)
      {
            printf(" ");
      }
      for (bricks = 1; bricks <= (i + 1); bricks++)
      {
            printf("#"); 
      }
        printf("\n");  
}
return 0;
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

4

Your first 'inner' for loop will run 'forever' (well, until integer overflow occurs), if it runs at all1. That is because, if the initial value of spaces (i - 1) is greater than or equal to zero, it will remain so (because you increment it at the end of the loop). You need, instead, to decrement spaces.

Further, your second 'inner' for loop will print only one side of the pyramid. Change the termination condition from bricks <= (i + 1) to bricks < (i * 2) to print both sides.

Here's a working version of the big for loop2:

    for (int i = 1; i <= n; i++) { // Subtle change here - exercise for the reader
        for (spaces = (n - 1); spaces >= i; spaces--) { // Note: spaces-- NOT spaces++
            printf(" ");
        }
        for (bricks = 1; bricks < (i * 2); bricks++) { // (i * 2) to get BOTH sides of the pyramid
            printf("#");
        }
        printf("\n");
    }

1 As it stands, when spaces starts of at zero, and assuming a 4-byte integer, the first of your inner loops will print just over 2 billion spaces before integer overflow occurs. That won't actually take forever – just quite a long time.

2 I made a couple of other 'minor' changes to your code! I appreciate your desire not to just copy other people's work, so maybe just add the changes I have mentioned/made, one at a time, to see their effects. Hopefully, this will help you understand how to get the desired output.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Thankyou that seems to have sorted the issue. So -- is simply the decrement function (-1). If i wanted to only accept a specific range of heights like 0<=n<=10, how would i go about this? And can i also ask why you put n < 1, when you can type any positive value as the height and it still runs? – William Cole Feb 03 '21 at 10:39
  • @WilliamCole A height (n) of 0 makes little or no sense. But you can change the `while` condition to `while (n < 1 || n > 10);` to limit the range of heights to `1 <= n <= 10`. The input loop will keep asking for input until that condition is *not* satisfied. – Adrian Mole Feb 03 '21 at 12:50