edited by
990 views
2 votes
2 votes

Consider the following $C$ implementation which when given $3$ numbers a,b,c as input, find the maximum of $3$ numbers $a,b,c.$

int kickstart(int a,int b,int c)
{
    if(B1) return a;
    if(a>=b) return B2;
    return kickstart(c,a,b);
}

How the boxes filled up correctly?

$I)B1:a\geq b$  &&  $a> c, B2:kickstart\left ( c,b,a \right );$

$II)B1:a\geq b$. &&. $a\geq c, B2:kickstart\left ( c,b,a \right );$

$III)B1:a\geq b$  &&  $a\geq c, B2:kickstart\left ( c,a,b \right );$

$IV)B1:a\geq b$  &&  $a\geq c, B2:kickstart\left ( b,c,a \right );$


Is it $I) and II)$ or $I) and IV)$

edited by

1 Answer

0 votes
0 votes

Concept

  1. Here the programming is checking if $a$ is greater than or equal to ($b$ and $c$) , if found true then it returns $a$ since $a$ is the maximum value.
  2. Otherwise it shuffles the values of $a,b$ and $c$ so that the maximum value gets alloted to $a$ and then it again goes to step 1 and check again.

Elimination method

option $I$ will go to infinite loop on giving $a=2,b=2,c=2$. due to $a>c$ condition.

option $II$ works fine since we replaced $a>c$ with $a\geq c$

option $III$ works fine.

option $IV$ will loop when $a=1,b=3,c=2$  $($  (1,3,2)$\rightarrow$(2,1,3)$\rightarrow$(1,3,2)$\rightarrow$(2,1,3).....and so on $)$

$\therefore$ option $II$ and option $III$ are correct.

edited by

Related questions

4 votes
4 votes
3 answers
1
2 votes
2 votes
2 answers
2
srestha asked May 12, 2019
1,074 views
Consider the following function $foo()$void foo(int n){ if(n<=0) printf("Bye"); else{ printf("Hi"); foo(n-3); printf("Hi"); foo(n-1); } }Let $P(n)$ represent recurrence r...