edited by
11,562 views
26 votes
26 votes

What is the value printed by the following C program?

#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;
}
  1. $-9$
  2. $5$
  3. $15$
  4. $19$
edited by

4 Answers

Best answer
27 votes
27 votes

Suppose $int$ array takes $4$ bytes for each element and stored at base address $100$.

Follow below image. Red color shows the return value.



So, $15$ is the answer.

Correct Answer: $C$

edited by
32 votes
32 votes
It will print
$12 + ( 7 - (13 - (4 + (11 - ( 6 + 0)))))$
$\quad = 12 + (7 - (13 - ( 4 + ( 11 -6)))))$
$\quad= 12 + 7 - 13 + 9$
$\quad= 15$
Answer:

Related questions

18 votes
18 votes
2 answers
1
18 votes
18 votes
3 answers
2
29 votes
29 votes
6 answers
3
Kathleen asked Sep 25, 2014
13,711 views
What value would the following function return for the input $x=95$?Function fun (x:integer):integer; Begin If x 100 then fun = x – 10 Else fun = fun(fun (x+11)) End;$...