> Please do write the explaination also.
Is this a homework assignment? Or did you just find code and are confused about the result.
Before the code is explained, it should be formatted better so it's easier to follow what's going on:
Code:
#include <stdio.h>
int f (int *a, int n)
{
if (n <= 0)
return 0;
else if ((*a % 2) == 0)
return *a + f(a + 1, n - 1);
else
return *a - f(a + 1, n - 1);
}
int main ()
{
int a[] = {12, 7, 13, 4, 11, 6};
printf("%d", f(a, 6));
return 0;
}
Now, go through each step of the program and note the variables each time. Start with something like this:
function call |
*a |
n |
return of f |
f(a, 6) |
12 |
6 |
12 + f(a + 1, n - 1) |
f(a + 1, n - 1) |
7 |
5 |
7 - f(a + 1, n - 1) |
f(a + 1, n - 1) |
13 |
4 |
13 - f(a + 1, n - 1) |