search
Log In
18 votes
3k views

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$
in Algorithms
edited by
3k views
0
@kenzou, The program won't compile with parenthesis used in the initializer. Please change it to "{ }"

5 Answers

14 votes
 
Best answer
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
9
In C leaglly it is possible to have 2 byte for "int". But there does not make any sense to have an int value which cannot go beyong 31267. So, please assume 4 bytes whenever we use int in C. Using 2 bytes for int is like making fun of C :(
1
Thanks a lot @arjun sir . I will keep this in my mind in future. I will update this IMG tomorrow.n

Edited @Arjun Sir
25 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$
11 votes

Answer : Option C) 15

5 votes
it is C. 15.
1 vote

int a[ ] = {12, 7, 13, 4, 11, 6}
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);


⇒12+(7-(13-(4+(11-(6)))))
⇒12+(7-(13-(4+5)))
⇒12+7-(4)
⇒12+3
⇒15

Answer:

Related questions

13 votes
3 answers
1
1.6k 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; } What is the return value of the function $\text{foo}$ when it is called as $\text{foo(513, 2)}$? $9$ $8$ $5$ $2$
asked Apr 21, 2016 in Algorithms jothee 1.6k views
13 votes
4 answers
2
2.7k views
Consider the code fragment written in C below : void f (int n) { if (n <= 1) { printf ("%d", n); } else { f (n/2); printf ("%d", n%2); } } Which of the following implementations will produce the same output for $f(173)$ as the above code? P1 P2 void f (int n) { if (n/2) { f(n/2 ... ("%d", n); } else { printf ("%d", n%2); f (n/2); } } Both $P1$ and $P2$ $P2$ only $P1$ only Neither $P1$ nor $P2$
asked Oct 29, 2014 in Algorithms Ishrat Jahan 2.7k views
8 votes
4 answers
3
1.6k views
Consider the code fragment written in C below : void f (int n) { if (n <=1) { printf ("%d", n); } else { f (n/2); printf ("%d", n%2); } } What does f(173) print? $010110101$ $010101101$ $10110101$ $10101101$
asked Oct 29, 2014 in Algorithms Ishrat Jahan 1.6k views
12 votes
4 answers
4
2.7k 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; } What is the return value of the function $\text{foo}$ when it is called as $\text{foo(345, 10)}$? $345$ $12$ $5$ $3$
asked Sep 29, 2014 in Algorithms jothee 2.7k views
...