recategorized by
2,172 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 */
recategorized by

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

3 votes
3 votes
3 answers
2
go_editor asked Nov 20, 2020
3,520 views
The number of positive integers not exceeding $100$ that are either odd or the square of an integer is _______$63$$59$$55$$50$
2 votes
2 votes
2 answers
3
go_editor asked Nov 20, 2020
3,241 views
How many ways are there to pack six copies of the same book into four identical boxes, where a box can contain as many as six books?$4$$6$$7$$9$