I am shuffling a deck of cards with c programming using an array and pulling from a function. I got the shuffle done but i need help find the ace of spades and king of spades. The array is just populated with cards 1 - 52. Ace of spades is card 52 and the king of spades is card 51. Can any help me find a way to find those cards after i have shuffled them?
Help with c programming?
So, you have put them into a completely random order. You can either go through and do a linear search, or you could create a structure where the card value maps to an index position in the shuffled deck.
A linear search would be more efficient if you wanted to find it once per shuffle.
An ordered map would be more efficient if you wanted to find it lots of times per shuffle, as it would involve computation to build, but you would not have to search it again to find the location of any card. For example, you can just get the location value of card 51.
Linear search:
int card = 51;
int position=-1;
for (int i =0; i%26lt;52; i++){
if (cards[i] == card){
position = i;
break;
}
}
This will exit with the position of card number 51, or -1 if not found.
Map:
int[52] cardmap;
for(int i =0; i%26lt;52; i++){
cardmap[cards[i]]=i;
}
This will create an array called cardmap, the position in the array cards of a card with value i can simply be obtained by getting the value at cardmap[i]. e.g. if card 51 was at position 4, cardmap[51] = 4.
Reply:That is going to be quite difficult. If you are still stuck may be you can contact a programming expert live at website like http://askexpert.info/
flower menings
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment