Let's recall the Division Algorithm, from Chapter 2. I will restate it here, in terms of the corollary that neatly sums up the complete result: Let a and b be integers, with b non zero. Then there exist unique integers q and r such that a = bq + r, with 0 <= r < |b| The problem here for normal computer use is that the operators / and % in C++ (and in other languages as well) do NOT act this way. If you set q = a / b, with r = a % b and a < 0, you will get a remainder that is negative. That is NOT what the theorem above wants! It ALWAYS wants a non-negative remainder! Now, to get that you will need to play with the "quotient". In general, if you have set q = a / b; r = a % b; and r is negative, you can add 1 to q if q > 0 or subtract 1 from q otherwise, and recalculate r = a - bq, and that will fix it. I want you to write a C++ program that will input a and b, calculate q and r this way and output results, testing whether a == bq + r with r being at least zero and less than |b|, and complaining if any of this is false. |
|
|
Q is -7 R is 1 |