I've been a programmer for decades, but for languages like Dyl-280 and SAS. I'm trying to get back into programming and am working my way through the Big Nerd Ranch Objective-C book. I ran into a program that I can't seem to understand. I know it must be simple, on some level, but it's eluding me...
The code is as shown here:
and it puts out the following:
What I'm having a hard time understanding is the flow of the program. It looks like the code tests for N being zero, and it does eventually get to zero, and then goes above it, but the real issue is the printf statement after the recursion. It looks like it is constantly calling itself, and looping through the if statement, and N is greater than zero at some point, and the code looks like it should then go back to recursion and just keep running off and looping until time freezes. Instead, it looks like it loops through the last printf statement of the else, and then the two outside the if, and then loops somehow back...
Setting breakpoints isn't helping because the breakpoints don't show that much different than the test output.
I guess I need a brain reboot to understand it. A different way of looking at it perhaps... It *looks like* strangeness is happening... It looks like it's looping at the wrong spots.
Help?
The code is as shown here:
Code:
unsigned long SumNumbersTo (unsigned long);
unsigned long sum;
int main (int argc, const char * argv[])
{
unsigned long N = 10;
unsigned long sum = SumNumbersTo (N);
printf ("Numbers from 1 to %3lu sum to %3lu.\n", N, sum); // debug print
return 0;
}
unsigned long SumNumbersTo (unsigned long N)
{
printf ("Pre if loop (N) (sum) %3lu, %3lu\n", N, sum); // debug print
if (N == 0) {
printf("If loop N = 0, N = %3lu, sum = %3lu\n", N, sum); // debug print
return 0;
} else {
printf("N greater than 0, PreRecurse sum value = %3lu\n", sum); // debug print
sum = N + SumNumbersTo (N-1); // Look recursion here!
printf ("Recurse N = %3lu, sum = %3lu\n", N, sum); // debug print
}
printf ("Sum numbers from 1 to %3lu = %3lu\n", N, sum);
printf ("N = %3lu, sum = %3lu\n", N, sum); // debug print
return sum;
}
and it puts out the following:
Code:
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.
What I'm having a hard time understanding is the flow of the program. It looks like the code tests for N being zero, and it does eventually get to zero, and then goes above it, but the real issue is the printf statement after the recursion. It looks like it is constantly calling itself, and looping through the if statement, and N is greater than zero at some point, and the code looks like it should then go back to recursion and just keep running off and looping until time freezes. Instead, it looks like it loops through the last printf statement of the else, and then the two outside the if, and then loops somehow back...
Setting breakpoints isn't helping because the breakpoints don't show that much different than the test output.
I guess I need a brain reboot to understand it. A different way of looking at it perhaps... It *looks like* strangeness is happening... It looks like it's looping at the wrong spots.
Help?
Last edited: