176 views

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 | 176 views
0

@Satbir

try it

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.

by Boss (20.9k points)
edited by
0
I think 3rd  will not go into loop
0

why u think ? @Kaluti

I have given the test case to proove it.

0
After 2 ,1 ,3 should not it again enter the function and it will satisfy 2 and condition then it will become 3 2 1 and it will satisfy first will output 3
0
yes u are correct. i will check it again and update the answer.
0

@Kaluti

@srestha