in Programming in C edited by
1,003 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)$

in Programming in C edited by
by
1.0k views

1 comment

@Satbir

try it 

0
0

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

5 Comments

I think 3rd  will not go into loop
0
0

why u think ? @Kaluti

I have given the test case to proove it.

0
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
1
1
edited by
yes u are correct. i will check it again and update the answer.
0
0

@Kaluti

@srestha

Please check my answer.

0
0

Related questions