3.3k views

Consider the C program fragment below which is meant to divide $x$ by $y$ using repeated subtractions. The variables $x$, $y$, $q$ and $r$ are all unsigned int.

while (r >= y) {
r=r-y;
q=q+1;
}

Which of the following conditions on the variables $x, y, q$ and $r$ before the execution of the fragment will ensure that the loop terminated in a state satisfying the condition $x==(y*q + r)$?

1. $(q==r) \ \&\& \ (r==0)$
2. $(x>0) \ \&\& \ (r==x) \ \&\& \ (y>0)$
3. $(q==0) \ \&\& \ (r==x) \ \&\& \ (y >0)$
4. $(q==0) \ \&\& \ (y>0)$

edited | 3.3k views
+3

Here, $x == ( y*q + r )$ says $q =$ quotient and $r =$  remainder.

To divide a number with repeated subtraction, quotient should be initialized to $0$ and should be incremented for each subtraction.

Initially $q=0 \Rightarrow r = x$.

$\therefore$ Initial conditions should be C] $(q == 0) \ \&\& \ (r == x) \ \&\& \ (y > 0)$.

by Active (4.5k points)
edited by
0
why do we need to check y>0 despite of it is given that y is unsigned integer.
+5

If we divide a number with 0 (zero), will result in undefined state. that's why we need to check y > 0 condition. And one more point unsigned integer range is 0 to 2n-1.

0
nice explanation @kantikumar
0
why quotient take 0 i dont understand plz clear??
+1
take an example x=2, y=3 then x divides y mean quotient q=0,remainder r=2 it becomes x, (r==x) and y is a divisor ,so it should be >0 otherwise whole expression will tend to infinity.
0
now i got the point thanku
0
this is a repeated question
0
how it is knows that the initial condition as r=x?
0
0
nice explanaion....
option c
by (147 points)
0
Why not option d? because when q==0 && y>0, it automatically means x==r, then why mention it explicitly?Please explain
0
How it automatically means x == r ??
0
x=(y*q+r) So, when q==0 and y>0 , then x=r...
+1
It is given in the question that we've to satisfy the condition x==(y∗q+r)
It is not given x=(y*q+r)
In the code fragment, initial value of r and q is not set. So, we've to initialize those.
r = x and q = 0.
0
Thankyou... i understood..

we can solve  by leting some values, as shown below:-

by (219 points)

by (221 points)
Just substitute the values in the variables.

let r=3, y>0 i.e,1 etc and q=0 and substitute in the each options then which one satisfied that is the answer.
by (11 points)

1
2