1

I'm writing a function that gets passed a pointer to an array of length 4. This array will contain integers 0 <= x <= 52 and I would like to construct an array of length 48 with every integer from da kine that's not in the passed in array. In python this would be

# just included for specificity
cards = [card for card in deck if card not in hand]

in C the best I can do is

int i, j, k, found_flag;
int cards[48];    /* int hand[4] is passed in */

k = 0;
for (i = 0; i < 52; i++) {
  found_flag = 0;
  for (j = 0; j < 4; j++) {
    if (i == hand[j]) {
      found_flag = 1;
      break;
    }
  }
  if (!found_flag) {
      cards[k++] = i;
  }
}

Is this optimal for this circumstance? Generally, is the 'go-to' pattern?

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • As currently implemented you can't get 48--51 into `cards`. Also, you have failed to initialize `k`. Finally, you should probably check that you don't over- or under-fill `cards`. Also, right now you're treating cards as a set (each bit on/off), but you may have meant `cards[k++] = i`. – dmckee --- ex-moderator kitten Aug 27 '10 at 05:40
  • @dmckee. Thanks for the corrections. That's what I get for not copy and pasting from my code (which actually runs). – aaronasterling Aug 27 '10 at 05:48
  • Use sets. `all_cards = set(range(52)); hand = [1, 10, 11, 44, 50]; deck = all_cards - set(hand)` – hughdbrown Jul 10 '11 at 22:11

4 Answers4

1

Could this be done this way?

cards_in_deck[48]={1};
for (int i=0;i<4;i++)
    cards_in_deck[hand[i]]=0;

The cards_in_deck is an array with value of 1 for those that are not in the deck. Is this what you are looking for ?

Gangadhar
  • 1,893
  • 9
  • 9
  • 1
    This is not correct, because `{1}` only initialises the *first* element of the array to 1. The rest are initialised to 0. – caf Aug 27 '10 at 05:45
  • +1 because this would have worked brilliantly (modulo the initialization concerns) for the code that I posted. Unfortunately, it had a few typos. – aaronasterling Aug 27 '10 at 05:50
  • ...and if he's going to use the set-like implementation for `cards` it will need to be 52 elements long. – dmckee --- ex-moderator kitten Aug 27 '10 at 05:50
  • This approach is essentially doing a radix sort on the hand. – caf Aug 27 '10 at 05:51
  • @caf you are right - sorry, forgot that initialization to zero is the standard @dmckee - I was not sure if the array really represented the card-deck. Yes you are right, it should be 52 @aaronasterling - you can use memset to set the value in cards_in_deck to 1. I guess that will resolve the initialization issue. – Gangadhar Aug 27 '10 at 05:54
1

Sure, your example is fine for a hand size of only 4 - it's clear enough. In situations where the arrays were much larger, then more efficient algorithms based on various kinds of sorting could be used.

For example, a radix sort eliminates the nested loops:

int i, j;
int card_in_hand[52] = { 0 };
int cards[48];    /* int hand[4] is passed in */

for (i = 0; i < 4; i++)
    card_in_hand[hand[i]] = 1;

j = 0;
for (i = 0; i < 52; i++)
  if (!card_in_hand[i])
      cards[j++] = i;
caf
  • 233,326
  • 40
  • 323
  • 462
0

Here's the little test program I put together to solve this one. It creates a set to show which cards are selected and scans through the set to build the array of the cards that are left.

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    int cardsSelected[4] = {3,7,10,49} ;
    int cardsLeft[48] ;
    int allCards[52] ;

    memset(allCards,0,sizeof(int)*52) ;
    for(int i= 0; i < 4; ++i) {
        allCards[cardsSelected[i]] = 1 ;
    }

    int k = 0 ;
    for(int i =0; i < 52; ++i) {
        if (!allCards[i])
            cardsLeft[k++] = i ;
    }

    for(int i = 0; i < 48; ++i) {
        printf("%d ", cardsLeft[i]) ;
    }
    printf("\n") ;

    return 0;
}
Jackson
  • 5,627
  • 2
  • 29
  • 48
0

In C, iterate over the sorted cards in hand:

int cards_in_deck[48];
const int ranges[6][2] = {
    {0,           hand[0]}, 
    {hand[0] + 1, hand[1]},
    {hand[1] + 1, hand[2]},
    {hand[2] + 1, hand[3]},
    {hand[3] + 1, hand[4]},
    {hand[4] + 1, 52}
};
int j = 0;
for (int i = 0; i < sizeof(ranges)/sizeof(ranges[0]); i++) {
    const int *range = ranges[i];
    for (int k = range[0]; k < range[1]; k++)
        cards_in_deck[j++] = k;
}

In python the code looks like this:

hand = [0, 10, 11, 40, 51]
ranges = [
    [0,           hand[0]], 
    [hand[0] + 1, hand[1]],
    [hand[1] + 1, hand[2]],
    [hand[2] + 1, hand[3]],
    [hand[3] + 1, hand[4]],
    [hand[4] + 1, 52]
]
cards_in_deck = [k for r in ranges for k in range(r[0], r[1])]
hughdbrown
  • 47,733
  • 20
  • 85
  • 108