2

This is the code I have, it works I don't know why and I have typed out my translation

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

int main(void)
{

   int height = 0;

   do {

      printf("Let's build a pyramid, what is the height \n");
    height = get_int();
    if (height == 0)

    {
        return 0;
    }
    }while (height <1 || height >23);

int height = 0 first I am telling C the number the user gives me for the height of the pyramid will be called "height"

if (height == 0) If the users height number is equal to zero then ask again for a pyramid height

}while (height <1 || height >23); but if the pyramid height is less than 1 that is the same as great than 23 and its OK to proceed to the next step (which makes no sense since the pyramid can not be less than 1 or greater than 23 for height)

NEXT STEPS

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

    {
        for (int j = 0; j< height -i-1;j++)
        {
            printf("%s", " ");
        }

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

for(int i = 0; i < height; i++) for this loop store and name a value i take the number given for the pyramids height, increase something by 1 and stop when the number of lines is equal to the number given for the pyramids height and end the loop

for (int j = 0; j< height -i-1;j++) for this loop store and name a value j and take the number for the pyramids height and decrease something by 1 and stop when number of lines equals the number for the pyramids height and end the loop

printf("%s", " "); %s is the value produced by for(int i = 0; i < height; i++) and will show up on the screen as spaces

for(int k = 0; k < i+2; k++) this loop will name and store a value as k and it will take the number or value of i and add 2 then it will create lines and increase the lines length by one and when the number of lines reaches the number given for the pyramids height it will stop

printf("#"); this set of lines will show up on the screen as hashtags and this will end this loop

printf("\n"); now send the cursor to the next line and we are done.

1

For the most part, you are correct, but it sounds like you might be confused on a few things.

}while (height <1 || height >23); but if the pyramid height is less than 1 that is the same as great than 23 and its OK to proceed to the next step (which makes no sense since the pyramid can not be less than 1 or greater than 23 for height)

You have to make sure that you are translating with context. On its own, this code might not make sense - like you said, the pyramid's height can't be less than one or greater than 23. However, this statement is part of a do/while loop, which does something while a certain statement is true. In mario.c, we want to continue to ask the user for the height if their input doesn't match what we want, which is for height to be greater than 1 and less than 23. To do this, we want to ask the user at least once for the height and then keep asking them if height < 1 || height > 23. Remember: we are checking to see if their input isn't what we want, which is why the while condition is height < 1 || height > 23.

if (height == 0) If the users height number is equal to zero then ask again for a pyramid height

There isn't a need for this part since height < 1 || height > 23 already covers the possibility that height could be 0.

for(int i = 0; i < height; i++) for this loop store and name a value i take the number given for the pyramids height, increase something by 1 and stop when the number of lines is equal to the number given for the pyramids height and end the loop

We can break this statement down to understand it fully.

int i = 0 This declares an integer variable called i and sets it to 0.

i < height This means that the loop will continue running as long as i is less than the value of height. When this condition becomes false (that is, when i is no longer less than height), the loop will terminate, or stop running.

i++ This means that we are going to increase the value of i by 1 every iteration, or loop. For example, for the first iteration, i will be 0; for the second iteration, i will be 1; and so forth. i will stop increasing when it is no longer less than height.

printf("\n"); now send the cursor to the next line

I just want to clarify that this doesn't necessarily send the cursor to the next line - this prints out a new, blank line.

I also wanted to add that in your statement printf("%s", " ");, you can just put printf(" ");. You don't necessarily need the %s because you are only printing out one thing, which you know the value of.

I hope this helps. If you have any more questions, please comment below!

| improve this answer | |

You must log in to answer this question.

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