+ 3
How there is a output 14?
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,5}; printf("%i",f(a,3)); }
1 Odpowiedź
+ 5
You're calling f() with int *a = 12 and n = 3.
f(12, 3)
n <= 0 ? false
*a % 2 == 0? true
=> return 12 + f(13, 2)
f(13, 2)
n <= 0 ? false
*a % 2 == 0? false
=> return 13 - f(14, 1)
f(14, 1)
n <= 0 ? false
*a % 2 == 0? true
=> return 14 + f(15, 0)
f(15, 0)
n <= 0 ? true
=> return 0
==> return 14 + 0 = 14