There are a few problems with your code, I'll try to point you in the right direction on the biggest mistake.
All of your for
loops use ==
as a loop-continuation condition. This is not normal, and is probably not doing what you expect it to.
for(r=0;r==n+1;r++)
{
...
}
In this loop, the first thing that happens is that r
is set to 0
. Then the loop condition r==n+1
is tested, and if it is TRUE, the loop executes. The problem in your code is that the loop will never execute.
Executing the loop for n == 1
(if the user input was 1 for number of levels):
r = 0 // r is set to 0
r == n + 1 --> 0 == 1 + 1 --> 0 == 2 --> False
Because the condition is false, the loop will not run. The loop will run only if that condition is True.
If you want a for
loop to run 10 times:
for (r = 0; r < 10; r++)
If you want a for
loop to run n
times:
for (r = 0; r < n; r++)
Another way to think about a for
loop is to convert it into a while
loop.
Your for
loop would look like this:
r = 0;
while (r == n + 1) { // this only runs if `r== n + 1`
...
r++;
}
It may be easier to understand what is happening as a while-loop.
a=;
You haven't set the initial value.