0 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$?

- $a^b$
- $b^a$
- $\lfloor \log_a^b \rfloor$ /* $\lfloor \: \: \rfloor$ means floor */
- $\lfloor \log_a^b \rfloor$ /* $\lceil \: \: \rceil$ means ceil */

0 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

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