MODULUS CONDITION NEED HELP!

INTRODUCTION OF CODE:
This is a small game, named Subtracting Game. here User and Computer Subtract the Specific Number that they decided to be subtracted from the Total and make it Zero. BTW nice game for Newbies.

Well short Intro.Please compile the CODE to understand what the Program do.
NEED LITTLE HELP:

Here is a Small Problem i dont Understand the only one Point of this CODE. and that is "remainder = total % (delta + 1);", OK i know About Modulus and knows what it does. The only thing that i dont know is That why the Programmer has added "1" to delta, actually what is his intention.

P.S: I am confused and don't understand this Coondition "remainder = total % (delta + 1);"...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
CODE :


#include <iostream>

using namespace std;

int main() {
    int total, n;
    int delta = 0;  // Max. number to subtract by each turn.
    int remainder = 0;
    
    cout << "Welcome to NIM. Pick a starting total: ";
    cin >> total;
    while (total < 1) {
         cout << "Enter positive integer only. " << endl;
         cout << "Pick starting total: ";
         cin >> total;
    }
    cout << "What should be the max. number to subtract? ";
    cin >> delta;
    while (delta < 1 || delta >= total) {
         cout << "Enter positive integer only. " << endl;
         cout << "Num must be less than starting total. " << endl;
         cout << "Pick max. number to subtract: ";
         cin >> delta;
    }
    while (true) {

         // Pick best response and print results.
         // Divide total by delta and use remainder
         //  as the number to subtract by.

         remainder = total % (delta + 1);
         
         if (remainder > 0) {
              total = total - remainder;
              cout << "I am subtracting " << remainder << "." << endl;
         } else {
              total--;
              cout << "I am subtracting 1." << endl;            
         }
         cout << "New total is " << total << endl;
         if (total == 0) {
              cout << "I win!" << endl;
              break;
         }

       // Get user’s response; must be in range 1 to delta.

         cout << "Enter number to subtract, ";
         cout << "from 1 to " << delta << ": ";
         cin >> n;
         while (n < 1 || n > delta) {
               cout << "Input must be 1 to " << delta << ". " << endl;
               cout << "Re-enter: ";
               cin >> n;
         }
         total = total - n;
         cout << "New total is " << total << endl;
         if (total == 0) {
              cout << "You win!" << endl;
              break;
         }
    }
    system("PAUSE");
    return 0;
}


SOURCE: "book" C++ WITHOUT FEAR BY BRIAN OVERLAND (EXERCISE#2.5.2)
Last edited on
If it was just
remainder = total % (delta)
the computer could never subtract the maximum.

There may be a deeper reason though. Taking the +1 out seems to make the game easier to win for the player.
can you Please explain this condition (remainder = total % (delta) ) ? what it does ?
That's no condition. this % (modulo) thing returns the offcut of a division. E.g. 12%5 would return 7, 5%12 would return 12.
remainder is the amount the computer subtracts.

remainder = total % (delta + 1); yields the optimal strategy to win for the computer.

When the on your turn you reduce total to (delta + 1) you have just won the game, providing you don't enter the wrong value. You win when the total reaches zero after your move.

For example, when the total is 10 and delta is 6, when you subtract the total to 7 (delta + 1), your opponent cannot win. This works for all multiples of 7, which is why modulus yields the best strategy for the computer.

The one flaw with this version of the game is that, as far as I can tell, the human player cannot win; because whoever goes first is guaranteed to win assuming they always make the optimal move, which in this case the computer is programmed to do and the comp. always goes first.
@FlashDrive: Your examples are wrong.
oh, are they? ... mmh, you're right. 12%5 is not 7, it's 2...
And 5%12 would be 5, not 12.
why that?

5/12=0 => 5%12=12-0=12
How is the remainder supposed to be larger than the original number? 5-0*12=5
Last edited on
i just test it:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main ()
{
  //similar
  cout << 3 % 21 << endl;
  cin.get();
  return 0;
}


probably, because the nearest value of 21 can get is 0 (the only closest value to 3, which meet the "less or same with value" condition)

so, the calculations would be:
1
2
3
4
5
6
     0
   _______
21/ 3
    0
  ______-
    3
Last edited on
The remainder formula is this:

a%b = a- int(a/b)*b

In other words, "Take 'b' out of 'a' as many times as you can. What remains is the remainder."

Again, in other words: a - a%b makes a perfectly divisible by b.

[edit]
(Forgot to add the point)
Thus, by definition, a%b <= a (because nothing is ever added, but can be subtracted) and a%b <= b (because else, you can 'take out' another b, violating the definition).
Last edited on
what do you mean by "violating the definition"?
The remainder a%b is the part of a that doesn't fit another b (or reversed: we can't take another b out of a%b). By definition, a%b <= b.
By definition, a%b <= b
do you mean, a%b <= a ? btw, how come a%b <= b ? it doesn't seems make sense to me...
(a%b) is the part of a that can't be divided into 'b'. Thus, not a single b fits into (a%b), thus a%b < b (should have been strictly smaller than, not less-or-equal).

Just take a look at the formula:

a%b = a - int(a/b)*b

If a%b <= a was not true, then int(a/b)*b would have to be negative. Since we limit the modulo operation to positive integers (do we? Might depend on your definition, but in either case you'd take the absolute value somewhere), that's not possible, thus a%b must be <= a. (It is equal to a if b > a, in which case the entire value of 'a' fits in 'b'.

If a%b < b was not true, then (a%b)/b > 1 (we can still take another b out of a%b). But that doesn't make sense, because int(a/b) is the full integer part of a divided by b, so (a%b)/b must be < 1.
Topic archived. No new replies allowed.