in Programming and DS recategorized by
2,144 views
2 votes
2 votes

Consider the following pseudo-code fragment, where $a$ and $b$ are integer variables that have been initialized:

/* Pre-conditions : $(a > 1 \wedge a < b)$ */

/* Assume that overflow never occurs */

int $x=0$; int $p=1$;

while $(p<b) \{$

$p=p*a$;

$x=x+1$;

$\}$

When the while loop terminates, what will be the value of $x$ in terms of $a$ and $b$?

  1. $a^b$
  2. $b^a$
  3. $\lfloor \log_a^b \rfloor$ /* $\lfloor \: \: \rfloor$ means floor */
  4. $\lfloor \log_a^b \rfloor$ /* $\lceil \: \: \rceil$ means ceil */
in Programming and DS recategorized by
2.1k views

1 Answer

2 votes
2 votes
assume a=2 (a>1) and b=15( a<b)

 

int x=0 , int p=1

while(p<15)   // executes 4 times

{

p=p*2   //

x=x+1

}

loop entry condition  p=1  {   p becomes 2  , x=1}   then  { p=2  p becomes 4, x=2}  

then { p=4 , p becomes 8  , x=3 } then  {p=8 , p becomes 16   x=4}

loop terminates

so final value of x=  ceil (lg15 ) =4  option d
Answer:

Related questions