edited by
12,033 views
27 votes
27 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

5 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

5.7k
views
2 answers
18 votes
go_editor asked Apr 21, 2016
5,720 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return 0; ... text{foo}$ when it is called as $\text{foo(513, 2)}$?$9$8$5$2$
9.1k
views
3 answers
18 votes
go_editor asked Sep 29, 2014
9,135 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return 0; ... {foo}$ when it is called as $\text{foo(345, 10)}$?$345$12$5$3$
14.2k
views
6 answers
30 votes
Kathleen asked Sep 25, 2014
14,231 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;$89$90$91$92$
13.5k
views
5 answers
27 votes
Kathleen asked Sep 22, 2014
13,545 views
Consider the following C-program:void foo (int n, int sum) { int k = 0, j = 0; if (n == 0) return; k = n % 10; j = n/10; sum = sum + k; foo (j, sum); printf ("%d,",k); } ... }$\text{8, 4, 0, 2, 0}$\text{2, 0, 4, 8, 14}$\text{2, 0, 4, 8, 0}$