Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



Consider the following pseudo-code fragment in which an invariant for the loop is $m \times x^k=p^n$ and $k\ge 0$.

Pre-conditions: $p\ge 1$ and $n\ge 0$

Assume that overflow never occurs.

int x = p;
int k = n;
int m = 1;

while (k != 0) {
if (k is odd) then m = m * x;
x = x * x;
k = floor(k / 2);
}

Which of the following must be true at the end of the while loop?






Solution

Given loop invariant is

$m \times x^k=p^n$

At the end of the while loop, condition becomes false.

So,

$k=0$

Putting $k=0$ in invariant,

$m \times x^0=p^n$

Since,

$x^0=1$

Therefore,

$m=p^n$



Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...