int main(void)
{
int height = 24;
while (height > 23 || height <=0)
{
printf("How tall do you want the tower to be?\n");
height = GetInt();
}
for(int a = 0; a < height; a++)
{
for(int c = a; c<=height; c++)
{
printf(" ");
}
for(int b = height - a; b<=height; b++)
{
printf("#");
}
printf("\n");
}
}
So, what I'm trying to do is have a tower that aligns with the left edge of the terminal window. For some reason though, this generates two extra spaces at the beginning of the "last" line (bottom of the tower). Even weirder, when I sit down with a pen and paper and manually run through the program, I'm showing that there should be on the first line, a number of spaces equal to "height" + 1, followed by one "#" then a new line, then a number of spaces equal to "height" followed by two "#", and so on. Why isn't my code evaluating like that, and what's with my two extra spaces? Sorry for the poor explanation.
If you want to repeat a certain piece of code a certain number of times, that is exactly where loops help you. We will go into how to use each loop in pseudocode, what each loop is used for, and examples for all 3 types.
The reason is that after the first iteration of this while loop (using a = 2), 'n' will become 3 and 'b' will become 36. Can become an infinite loop which means that the compiler will never come around to the final 'printf' statement.
Originally Answered: Why is my code not printing an output? This is because your code never exits out of the while loop, especially if a is small (e.g. a=3). I’m not sure what you’re trying to do in this piece of code (n = 6^a ?
Iteration can play a huge part in algorithms, whether you are making a quiz app, a calculator or even working with databases. But the thing is, iteration is much, much simpler than it seems! In this guide we will be covering For loops in pseudocode, While loops in pseudocode and even Do loops in pseudocode. What can loops be used for in pseudocode?
It's because you print height + 1
at the beginning of each line whereas you want to print height-1
spaces.
Change your condition from:
for(int c = a; c<=height; c++)
to
for(int c = a; c<height-1; c++)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With