# Don't uderstand how it's working

This is a discussion on Don't uderstand how it's working within the C Programming forums, part of the General Programming Boards category; Hi, I don't understand how this recursion is working , I need to convert it to iterative function but without ...

1. ## Don't uderstand how it's working

Hi, I don't understand how this recursion is working , I need to convert it to iterative function
but without understanding i can't

Code:
```double sequence(int n)
{
double sum,i;

if(n <= 30) return 2*n;
else
{
for(sum =0,i = 1;i <= 30;i++)
sum+= sequence(n-i);

return sum/30;
}
}```

2. Looks to me like sum will be the value of n = 1...30 * 2^n . Maybe

 that's wrong, I missed the (n-i) bit

I would throw some printf's in a couple places so you can watch it work.

3. Get out a piece of paper and work through some cases. n < 30 are your base cases, so that's easy. Look at what happens for 31 and 32 and 33. Maybe also look at 61, 62, 63....

4. Try working out what happens when this code is run.
Code:
```#include <stdio.h>
void level(unsigned int n);

int main(void) {
printf("In main. Going down!\n");
level(4);
printf("Back in main!\n");

return 0;
}

void level(unsigned int n) {
printf("Level %d, going down.\n", n);
if (n > 0) {
level(n - 1);
}
printf("Level %d, going up.\n", n);
}```

Popular pages Recent additions