search
Log In
0 votes
42 views

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
42 views

1 Answer

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

Related questions

1 vote
0 answers
1
38 views
Suppose you are compiling on a machine with $1$-byte chars, $2$-byte shorts, $4$-byte ints, and $8$-byte doubles, and with alignment rules that require the address of every primitive data element to be an integer multiple of the element's size. Suppose further that the compiler is not ... ; double r; int i; } A[10]; /*10 element array of structs */ $150$ bytes $320$ bytes $240$ bytes $200$ bytes
asked Nov 20 in Programming and DS jothee 38 views
0 votes
1 answer
2
124 views
The number of positive integers not exceeding $100$ that are either odd or the square of an integer is _______ $63$ $59$ $55$ $50$
asked Nov 20 in Set Theory & Algebra jothee 124 views
1 vote
1 answer
3
64 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$
asked Nov 20 in Combinatory jothee 64 views
0 votes
1 answer
4
52 views
Which of the following pairs of propositions are not logically equivalent? $((p \rightarrow r) \wedge (q \rightarrow r))$ and $((p \vee q) \rightarrow r)$ $p \leftrightarrow q$ and $(\neg p \leftrightarrow \neg q)$ $((p \wedge q) \vee (\neg p \wedge \neg q))$ and $p \leftrightarrow q$ $((p \wedge q) \rightarrow r)$ and $((p \rightarrow r) \wedge (q \rightarrow r))$
asked Nov 20 in Discrete Mathematics jothee 52 views
...