One way to compute the quotient and remainder of two numbers num and den is to use repeated subtraction as shown in the following code:
int q = 0; // the quotient
int rein = num; // the remainder
while (rem >= den) {
rem >= den) {
rem = rem- den;
q++;
} // end while
The invariant for this code is
rem >= 0 and
num = q * den + rem
For the values nurn = 17, den = 4, prove that the invariant is true before the loop begins, after each iteration (pass) of the loop, and when the loop terminates by compledng the following table (you may not need all passes shown), lhe initial values are shown:
pass | q | rem | rem >= 0 | num = q * den + rem |
initially 1 2 3 4 5 | 0
| 17
| true
| 17 = 0 * 17 + 17, true
|
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.