Thread: Find card number from deck

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    15

    Find card number from deck

    I was given an assignment to simulate a deck of 52 playing cards. I am required to shuffle the deck and all of this must be done using information learned previously in the class thus far. We have made it to arrays, not pointers, and all the basics up to that point. I was able to create the deck of cards using arrays and shuffle them. The next part of the assignment was to determine the location, card number, of the Ace of Hearts. This means that after the deck is shuffled what element of the array is the Ace of hearts located in and report that card number. This is the part I am having trouble with. Here is my code thus far.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <Stdlib.h>
    
    
    void shuffle (int deck[], int array_size);
    int Ace_Hearts (int deck, int value[], int suit[]);
    int Queen_Clubs(int deck, int array_size);
    
    
    
    
    int main (void)
        {
    
    
            int deck[52];
            int i;
            int card;
            char suit[4][9]={"Diamonds","Hearts","Clubs","Spades"};
            char value[13][7]={"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
            srand(time(NULL));
    
    
            for (i=0; i<52; i++)
                deck[i]=i;
    
    
            for(i=0; i<52;i++)
            printf( "%s of %s\n" , value[deck[i] % 13] , suit[deck[i] / 13] ) ;
    
    
    
    
            shuffle(deck,52);
            printf("\n\n\n");
    
    
            for(i=0; i<52;i++)
            {
                printf( "%s of %s\n" , value[deck[i] % 13] , suit[deck[i] / 13] ) ;
            }
    
    
        }
    
    
    void shuffle(int deck[], int array_size)
        {
            int i , j , temp ;
    
    
            for( i = 0 ; i < array_size ; i ++ )
                {
                    j = rand() % array_size ;
                    temp = deck[i] ;
                    deck[i] = deck[j] ;
                    deck[j] = temp ;
                }
        }
    
    
    int Ace_Hearts (int deck, int value[], int suit[])
        {
            int i=0;
    
    
                           
        }
    I am supposed to create a new function to find the Ace of Hearts. I do not know how to look at all the scrambled deck and determine to stop at the Ace of Hearts. I feel like a for loop is needed. Thanks for any help possible.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You need to know how Ace of Hearts is expressed in your deck (which number between 0 and 51). Then loop through the whole deck and stop when you come across Ace of Hearts.

    Does this help?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    It does make sense to do that, but how would I go about it. I do not know how to use the array function as a conditional statement.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Example using an array in a condition.
    Note: The number 42 is NOT likely the correct number needed.

    Tim S.

    Code:
    /* include headers */
    
    #define ACE_HEARTS 42
    
    /* code needed */
    
    if (ACE_HEARTS == deck[i]){
      /* do something */
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    That does make perfect sense and it worked perfectly. Thank you. Now another question. Say I wanted to input a card number and get the card at that position in the deck. Say I pick 40, the program will output the card at that position. I am thinking I could set the shuffled deck equal to an array that increases by one and goes 0,1,2,4...51. Then just create a for loop to determine the card at the number chosen minus 1, by looking at the array. I have tried this and it did not work. Is there a better method of going about this?

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    Never mind I ended up figuring it out. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-25-2012, 09:47 AM
  2. C programming homework (SHUFFLING A DECK OF CARD)
    By arm3103 in forum C Programming
    Replies: 11
    Last Post: 10-16-2011, 03:22 PM
  3. [Help] Program : How to validate credit card number
    By kingofdcp in forum C Programming
    Replies: 13
    Last Post: 10-31-2009, 12:58 AM
  4. Getting the number of DVI connections on a video card
    By Night_Blade in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2007, 06:53 AM
  5. Replies: 2
    Last Post: 11-07-2003, 12:21 AM