edited by
1,068 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

1.1k
views
3 answers
4 votes
srestha asked May 20, 2019
1,066 views
#include<stdio.h> #include<iostream> int bar(int m, int n){ if(m==0)return n; if(n==0)return m; return bar(n%m,m); } int foo(int m,int n){ return(m* ... { int x=foo(1000,1500); printf("%d",x); return 0; }Output of the program is ___________
1.2k
views
2 answers
2 votes
srestha asked May 12, 2019
1,169 views
Consider the following function $foo()$ ... . Can someone explain the options well. Moreover , what will be constant added $1$ or $2?$
1.1k
views
0 answers
1 votes
srestha asked May 10, 2019
1,091 views
Consider the following C code:int getNextGap(int gap){ gap=(gap*10)/13; if(gap<1)return 1; return gap; } void mystery(int a[ ],int n){ int gap= ... last?I mostly stuck how bool function working here. Plz help me out, how program executing:(
963
views
0 answers
1 votes
srestha asked May 8, 2019
963 views
Consider the following function int fun(int a[ ],int l, int target){ int i=0,j=0,sum=0,count=0; while(j<l){ if(sum<target){ sum=sum+a[j]; j++; ... $6,$ but I got $4.$ Which one correct?? Any shortcut to evaluate??