0

So after fiddling with this for about 2 days, I managed to get a working code that gives me the result I wanted, but the last part was pure luck and frustration as I entered random variables to see what gave me the result I wanted. Now, I don't understand why it is giving me the right result:

include

include

int main(void)

{

int height;

do

{

    height = get_int("Enter the height of the pyramid.\n");

}

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

for (int i = 0; i < height; i++)

{

for (int space = (height - 1); space > i; space--)

{

    printf("x");

}

for (int hash = 0; hash <= i ; hash++)

{

    printf("#");

}

printf("\n");

} }

For the spaces, why would the space be depended on 'i'?

From my understanding, 'i' will start at 0 (from the outer for loop) and increase by 1 until it is the same number as the height (so if the height entered was 3, 'i' will stop at 2 (0, 1, 2).

So if my space-- is dependent on 'i', in an example where the user enters a value of '4', based on my code above:

First row will initiate with 3 spaces (4-1), check if its more than 0 (the initial value of 'i'), and decrease the space by 1 if True.

Second row will now start with 2 spaces, check if its more than 1 (the current value of 'i' as i++ from the first loop), and decrease by 1 again if True

Third row will now start with 1 space, check if its more than 2 (the current value of 'i' as i++ from the second loop), this will equate to False (as 1 space is less than the 'i' value of 2), so the loop will stop meaning the 3rd row never gets printed.

So from my user who entered a height of 4, the program should only return 2 rows? I don't understand why this program is working fine.

Also why would the hash start from 0? Surely the first row needs to start with 1 hash?

1
Height: 4
   *
  **
 ***
****

1st loop of parent loop
i = 0; 
space = height - 1, = 3;
space > i??? TRUE
space -- print ' '
space = 2;
space > i??? TRUE
space -- print ' '
space = 1
space > i?? TRUE
space -- print ' '
space = 0
space > i?? FALSE 
HASH LOOP 
hash = 0;
hash <= i??? TRUE
print '#' hash++
hash = 1;
hash <= i??? FALSE
NEW LINE 
i < height?? TRUE
print new line
i++ = 1;
2nd loop of parent loop
i = 1;
space loop
space = height - 1 = 3;
space > i??? TRUE
space -- print ' '
space = 2;
space > i??? TRUE;
space -- print ' '
space = 1;
space > i??? FALSE;
HASH LOOP 
hash = 0;
hash <= i??? TRUE;
print '#' hash++;
hash = 1; 
hash <= i??? TRUUE;
print '#' hash++;
hash = 2;
hash <= i??? FALSE;
NEW LINE
i < height?? TRUE;
print new line

And so on, sometimes you can just step back and analyze your code line by line, sometimes using papers and writing the output as you go helps to understand your code, and don't worry as you go along the course you will be taught how to use something ccalled a debugger it's a tool that really helps to go through your code 1 by 1, GOODLUCK in cs50

| improve this answer | |
  • Thanks @ojou Nil! I actually managed to go through the Mario stages, now onto cash! – CodingNewb Apr 16 at 20:15

You must log in to answer this question.

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