views:

56

answers:

3

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?

+1  A: 

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
This is not correct, because `{1}` only initialises the *first* element of the array to 1. The rest are initialised to 0.
caf
@Gangadhar +1 because this would have worked brilliantly (modulo the initialization concerns) for the code that I posted. Unfortunately, it had a few typos.
aaronasterling
...and if he's going to use the set-like implementation for `cards` it will need to be 52 elements long.
dmckee
This approach is essentially doing a radix sort on the hand.
caf
@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
+1  A: 

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
see my corrections. Sorry for the confusion.
aaronasterling
A: 

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