retagged by
13,434 views
41 votes
41 votes

Consider the following program:

int f (int * p, int n) 
{ 
    if (n <= 1) return 0; 
    else return max (f (p+1, n-1), p[0] - p[1]); 
}
 int main () 
{
    int a[] = {3, 5, 2, 6, 4}; 
    printf(" %d", f(a, 5)); 
}

Note: $\max (x, y)$ returns the maximum of $x$ and $y$.

The value printed by this program is ________.

retagged by

6 Answers

Best answer
65 votes
65 votes

$f(a,5)$

  • $p$ , $n=5.$ $\begin{array}{|l|l|l|l|l|} \hline 3 & 5 & 2 &  6 &   4 \\\hline \end{array}$
    $\max(f(p+1,5-1),3-5) = \max(f(p+1,4),-2)$
  • $p$ , $n=4. \begin{array}{|l|l|l|l|} \hline 5 & 2 &  6 &   4 \\\hline \end{array}$
    $\max(\max(f(p+1,4-1),5-2),-2)=\max(\max(f(p+1,3),3),-2)$
  • $p$ , $n=3. \begin{array}{|l|l|l|} \hline 2 &  6 &   4 \\\hline \end{array}$
    $\max(\max(\max(f(p+1,3-1),2-6),3),-2) =\max(\max(\max(f(p+1,2),-4),3),-2)$
  • $p$ , $n=2.\begin{array}{|l|l|} \hline 6 &   4 \\\hline \end{array}$
    $\max(\max(\max(\max(f(p+1),1),2),-4),3),-2)$
  • $n=1$, return $0$

$\max(\max(\max(\max(0,2),-4),3),-2)$

$=\max(\max(\max(2,-4),3),-2)$

$=\max(\max(2,3),-2)$

$=\max(3,-2)$

$=3$

edited by
38 votes
38 votes

Ans is 3

Answer:

Related questions

29 votes
29 votes
7 answers
1
Akash Kanase asked Feb 12, 2016
8,796 views
The value printed by the following program is _______.void f (int * p, int m) { m = m + 5; *p = *p + m; return; } void main () { int i=5, j=10; f (&i, j); p...
50 votes
50 votes
4 answers
4
Akash Kanase asked Feb 12, 2016
17,628 views
The width of the physical address on a machine is $40$ bits. The width of the tag field in a $512$ KB $8$-way set associative cache is ________ bits.