The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
0 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)$

asked in Programming by Veteran (111k points)
edited by | 89 views


try it 

1 Answer

0 votes


  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.

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

why u think ? @Kaluti

I have given the test case to proove it.

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



Please check my answer.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,540 questions
54,099 answers
71,006 users