I think in the future you should do what Matticus said. In fact, do it any way even if you understand this after reading what I wrote. Personally, I bought myself a whiteboard and I use it ALL THE TIME when I am learning a new programming concept. This helps to save paper, is more fun (IMO), and allows me to quickly and easily focus on specific subjects. I recommend going to a store and getting a decent sized whiteboard. You dont even have to mount it on a wall if you dont want. Plus, the added benefit to this is a lot of employers ask you to "whiteboard" stuff out now adays which will be 2nd nature to you.
So.....
Code:
int height = 0;
// asks for input, rejects everything besides 1-23
do
{
printf ("Height of the pyramid: ");
height = GetInt();
}
while (height <= 0 || height > 23);
int height = 0; is creating a new integer variable and initializing it to the value of 0.
After this is a do while loop that is saying "Continuously prompt the user for the height of the pyramid UNTIL the user enters a height which is between 0 and 23."
As soon as a user enters a number between those two numbers, the do while loop will exit and continue to the next set of instructions. It is possible that the do while loop will never actually loop because if the user enters a height between 0 and 23 the first time, it will not satisfy the conditions needed to loop over again.
Code:
for (int row = 0; row < height; row++) {
// creates height-1 spaces
for (int space = height - 2; space > row - 1; space--)
{
printf("%c", ' ');
}
// creates height+2 hashtags
for (int pound = 0; pound < row + 2; pound++)
{
printf("#");
}
printf("\n");
}
I don't feel like me walking you through all of this is necessary and will even help you understand it any better. In fact, if I literally translate it to English like you wanted, this part will sound quite confusing. So this is what you need to know:
- The moral of the story here is that this logic is taking whatever number the user entered above and it's obviously drawing some characters to the screen based upon that number.
- I suggest running the program, entering a number, and looking at the output side-by-side with the code and paying very close attention to the terms in the for loops.
For example:
Code:
for (int space = height - 2; space > row - 1; space--)
Stare at this code and then ask yourself "What is space equal to right now? 0. Okay, what is row equal to? Now is space greater than row -1? Ok, if so, then the loop will progress through its next cycle, then it will subtract 1 from space and check the condition again... It will keep repeating the code between the {}(looping) until space is no longer greater than row. In reality, this could mean that space EQUALS row - 1, or it could mean that space is LESS than row - 1. If space is either equal to OR less than row - 1, this means that space is NOT greater than row - 1, thus the loop stops executing. Btw I would write it like (row - 1) to avoid confusion in the code.
The bigger picture here is that this program uses nested for loops, and what that means is that for every single iteration (0 - n times, n being the number entered by the user earlier) of the external (first) for loop, the inner loops will also execute all of their cycles. Keep in mind that in programming in general, it is possible for the inner loops to NOT execute if their conditions are not met.
So on cycle one of the outer loop, the inner loop will cycle a bunch of times, then the 2nd inner loop will cycle a bunch more times, now the outer loop will move to the next cycle and the inner loops will cycle a bunch of times again. If you are still totally lost after doing both suggestions, then get a debugger and put some breakpoints on the loops, then step through the entire program and watch where the execution goes. This will literally show you step-by-step what is going on in a way that will be almost impossible to not understand. CodeBlocks can do this, or you could use the gdb debugger. If you dont know anything about this, get on YouTube and find some C debugging tutorials.