Not Yet Answered # cards

TkTkorrovi 69 dwks 149 TkTkorrovi 69 Discussion Starter rowly WaltP 2,905 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

Yes. As i understand it's something about card game, these calculations are likely used for generating some random numbers. The % operator gives the remainder, so i % 13 always gives a number from 0 to 12, if i is positive, no matter what value i has. The << operator increases the number by shifting it left by one binary place, which means that << 1 is the same as multiplying by 2, << 2 the same as multiplying by 4, but a left shift is much faster than multiplying. What this gives is a random number which changes by a certain bigger unit, say by 4, which is sometimes necessary for a certain random behaviour. And then, i += i / 13 is just the same as i = i + i / 13...

0

The OP's code is also missing a closing curly bracket (**}**) at the end of the for loop.

This code is equivalent, but simpler:

```
for (i = 0; i < 52; i++)
{
deck[i] = i % 13; /* i modulo 13 */
deck[i] = i * 2;
deck[i] = deck[i] + i / 13;
if(i/13 < 2)
deck[i] = deck[i] + 64;
}
```

The modulo operator is explained in detail here: http://en.wikipedia.org/wiki/Modulo_operation

0

I see now that this code doesn't generate random numbers, but some kind of distribution, still the same applies.

0

wel this code fit inside a fuction which fill a deck of cards (52 cards)

knowing that ->

```
static char *s[] = {"Hearts","Diamonds","Clubs","Spades"};
static char *v[] = {"Ace","Two","Three","Four","Five","Six",\
"Seven","Eight","Nine","Ten","Jack",\
"Queen","King"};
static char *c[]= {"Black","Red"};
```

actually i cant understand the logic behind this function

is it like creating a memory space for 52 cards so we can deal with it later or just creating cards...

because there is 2 other functions that show you the deck, shuffle the deck and print the deck again(shuffled).

```
for (i = 0; i < 52; i++)
{
deck[i] = (i % 13)<<2;
deck[i] += i / 13;
if(i/13 < 2)
deck[i] += 64;
}
```

0

Actually, I don't understand it, either. The shifting and adding 64 makes no sense.

Since a deck has 52 cards, the loop is going through all the possible cards of the deck (0-51). Then `(i / 13)`

gives you 0-4, representing the suit of the given card. `(i % 13)`

gives the value or rank of the card (0-12). Therefore, if * i* is 5 you get the

If you want to shuffle the deck, you can then yse the random generator to move cards to different random positions.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...