[code]unsigned long SumNumbersTo (unsigned long N)
{
printf (“Pre if loop (N) (sum) %3lu, %3lu\n”, N, sum);
if (N == 0) {
printf(“If loop N = 0, N = %3lu, sum = %3lu\n”, N, sum);
return 0;
} else {
printf(“N greater than 0, PreRecurse sum value = %3lu\n”, sum);
sum = N + SumNumbersTo (N-1); // Look recursion here!
printf (“Recurse N = %3lu, sum = %3lu\n”, N, sum);
}
printf (“Sum numbers from 1 to %3lu = %3lu\n”, N, sum);
printf (“N = %3lu, sum = %3lu\n”, N, sum);
return sum;
}[/code]
I did the above, and got this as output:
Pre if loop (N) (sum) 10, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 9, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 8, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 7, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 6, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 5, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 4, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 3, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 2, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 1, 0
N greater than 0, PreRecurse sum value = 0
Pre if loop (N) (sum) 0, 0
If loop N = 0, N = 0, sum = 0
Recurse N = 1, sum = 1
Sum numbers from 1 to 1 = 1
N = 1, sum = 1
Recurse N = 2, sum = 3
Sum numbers from 1 to 2 = 3
N = 2, sum = 3
Recurse N = 3, sum = 6
Sum numbers from 1 to 3 = 6
N = 3, sum = 6
Recurse N = 4, sum = 10
Sum numbers from 1 to 4 = 10
N = 4, sum = 10
Recurse N = 5, sum = 15
Sum numbers from 1 to 5 = 15
N = 5, sum = 15
Recurse N = 6, sum = 21
Sum numbers from 1 to 6 = 21
N = 6, sum = 21
Recurse N = 7, sum = 28
Sum numbers from 1 to 7 = 28
N = 7, sum = 28
Recurse N = 8, sum = 36
Sum numbers from 1 to 8 = 36
N = 8, sum = 36
Recurse N = 9, sum = 45
Sum numbers from 1 to 9 = 45
N = 9, sum = 45
Recurse N = 10, sum = 55
Sum numbers from 1 to 10 = 55
N = 10, sum = 55
Numbers from 1 to 10 sum to 55.
It didn’t make it much clearer…
I will move on though.