I'M working my way through a C++ book and now I'M doing a card deck program. Here's the line I'M having trouble with.

r = card % 13;     // r = random 0 to 12

card in this case is just a random int number from 0 to 51.

52 diveded by thirten is 4. So if the % operator returns the remainder of a random number from 0 to 51 then as far as I can tell it's always going to be a decimal, is it not? Not a whole number from 0 to 12 as the comment would have me believe. Also, how can an int even be a deciaml? Sorry, I know there something really simple I'M missing here.

Recommended Answers

All 2 Replies

%, as you've stated is the remainder of a division operation. If you do long-hand division of two numbers, the value you have left at the end of the division is the remainder - this is always a whole number. Consider the following:

   ___1_
 7 | 12
      7
    ---
      5 <- this is the remainder

Thus, 12 / 7 is 1 and 12 % 7 is 5 (the remainder).

a = q*b + r, where 0<=r<b,

then a/b=q and r is the remainder, for example
5 = 2 * 2 + 1
so 5/2=2 and 5%2=1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.