Consider the following pseudo-code fragment, where a and b are integer variables that have been initialized: /* Pre-conditions : (a > 1 ∧ 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 ?

Consider the following pseudo-code fragment, where a and b are integer variables that have been initialized: /* Pre-conditions : (a > 1 ∧ 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 ? Correct Answer <span class="math-tex">\(\left[ {\log _a^b/*} \right]\left[ \; \right]means\;ceil*/\)</span>

The correct answer is option 4

Explanation

Since  a > 1 and a < b

int x=0 , int p=1

Let us assume a=2 and b=15

Whenever the loop is entered, the value of p is doubled and the value of x is incremented by 1.

In iteration 1: p = 2, x = 1

In iteration 2: p = 4, x = 2

In iteration 3: p = 8, x = 3

In Iteration 4: p = 16, x = 4

Final values are a = 2, b = 15, p = 16, x = 4

From the above values, it can be seen that x =

Related Questions