Thread: C programming homework (SHUFFLING A DECK OF CARD)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    C programming homework (SHUFFLING A DECK OF CARD)

    hi, i have to create a program that will shuffle a deck of cards, i am just missing a function but i am still getting an error on the last part, can someone please help me? Thank you.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define NCARDS 52
    #define NPROPS 2
    #define NSUITS 4
    #define NFACES 13
    char* suit[NSUITS]={"hearts","spades","clubs","diamonds"};
    char* face[NFACES]={"ace","two","three","four","five","six","seven","eight","nine",
    "ten","jack","queen","king"};
    void PrintCard(int deck[NCARDS][NPROPS], int i);
    void InitDeck(int deck[NCARDS][NPROPS]);
    void SwapCards(int deck[NCARDS][NPROPS], int src, int dest);
    void ShuffleDeck(int deck[NCARDS][NPROPS]);
    int GetPlayValue(int deck[NCARDS][NPROPS], int i);
    
    
    int main()
    {
        int deck[NCARDS][NPROPS];
        int src;
        int dest;
        int i;
        srand(time(NULL));
        InitDeck(deck);
        ShuffleDeck(deck);
        SwapCards(deck, src, dest);
        for (i=0; i<NCARDS; i++)
        {
            PrintCard(deck,i);
        }
        GetPlayValue(deck,i);
    
        return 0;
    }
    
     void InitDeck(int deck[NCARDS][NPROPS])
    {
        int suit;
        int face;
        int row;
        for (suit=0; suit<4; suit++)
        for (face=0; face<13; face++)
        {
            deck[row][0]= suit;
            deck[row][1]= face;
             row++;
        }
    void ShuffleDeck(int deck[NCARDS][NPROPS]);
    {
        int src, dest, i;
        for (i=0; i<NCARDS; i++)
        {
            src = i;
            dest = rand()%NCARDS;
            SwapCards(deck, src, dest);
    
        }
    }
     void SwapCards(int deck[NCARDS][NPROPS], int src, int dest);
    {
        int temp;
        int src;
        int dest;
        temp = deck[src][0];
        deck[src][0] = deck[dest][0];
        deck[dest][0] = temp;
        temp = deck[src][1];
        deck[src][1] = deck[dest][1];
        deck[dest][1] = temp;
    
    }
    void PrintCard(int deck[NCARDS][NPROPS], int i);
    {
        int tempsuit;
        int tempface;
        int i;
        tempsuit = deck[i][0];
        tempface = deck[i][0];
        printf("Card %d = %s of %s\n", i, suit[tempsuit], face[tempface]);
        GetPlayValue(deck,i);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    lines 49, 60, 73
    All have a ; at the end, which they shouldn't have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Thank you. Could you help me with line 80?? i dont get what im doing wrong!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have two variables with the same name: i.

    One is the parameter i, and the other is the local function i.

    Give them unique names.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    by doing that i change the meaning of function. could u give me a example? because i thought that doing that will matter since they are being declared in 2 diff functions.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you have two variables with the same name, one from the local function, and one from another function, the local variable will be given the address inside the function, effectively "hiding" the non-local version of the variable.

    That has been my experience with it, but the official standard, I believe, says the result is undefined. In any case, it's an error in logic and clarity in the code. You will not change the meaning of the function, you still have one i variable (the parameter). That's the one you keep. Delete the other declaration of i.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    when i change the variable i from that function just everything else goes crazy. some variables get undeclared i dont know

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, that's why you ask, and I do know. < sorry, but facts is facts >

    What errors are you getting? Paste 'em up here! Did you fix the errors that Salem mentioned earlier in the thread?

    Maybe you better upload the code, so I can load it up and see what's doing, and be sure I'm looking at the current code version.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    I have to create the same program. I edited the one by arm3103 a little bit so I don't get any errors, but now the cards shuffle from ace to four. Help?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define NCARDS 52
    #define NPROPS 2
    #define NSUITS 4
    #define NFACES 13
    
    char* suit[NSUITS]= {"hearts","spades","clubs","diamonds"};
    char* face[NFACES]= {"ace","two","three","four","five","six","seven","eight","nine",
                         "ten","jack","queen","king"
                        };
    void PrintCard(int deck[NCARDS][NPROPS], int i);
    void InitDeck(int deck[NCARDS][NPROPS]);
    void SwapCards(int deck[NCARDS][NPROPS], int src, int dest);
    void ShuffleDeck(int deck[NCARDS][NPROPS]);
    int GetPlayValue(int deck[NCARDS][NPROPS], int i);
    
    int main()
    {
        int deck[NCARDS][NPROPS];
        int i;
        srand(time(NULL));
        InitDeck(deck);
        ShuffleDeck(deck);
        for (i=0; i<NCARDS; i++)
        {
            PrintCard(deck,i);
        }
        return 0;
    }
    
    void InitDeck(int deck[NCARDS][NPROPS])
    {
        int suit;
        int face;
        int row;
        row = 0;
        for (suit = 0; suit<4; suit++)
            for (face = 0; face<13; face++)
            {
                deck[row][0] = suit;
                deck[row][1] = face;
                row++;
            }
    }
    
    void ShuffleDeck(int deck[NCARDS][NPROPS])
    {
        int src, dest, i;
        for(i = 0; i<NCARDS; i++)
        {
            src = i;
            dest = rand()%NCARDS;
            SwapCards(deck,src,dest);
        }
    }
    
    void SwapCards(int deck[NCARDS][NPROPS], int src, int dest)
    {
        int temp;
        temp = deck[src][0];
        deck[src][0] = deck[dest][0];
        deck[dest][0] = temp;
        temp = deck[src][1];
        deck[src][1] = deck[dest][1];
        deck[dest][1] = temp;
    }
    
    void PrintCard(int deck[NCARDS][NPROPS], int i)
    {
        int tempsuit, tempface;
        tempsuit = deck[i][0];
        tempface = deck[i][0];
        printf("Card %d = %s of %s\n", i, suit[tempsuit], face[tempface]);
    }
    Last edited by anonymau5; 10-16-2011 at 02:45 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok guys ... why is your deck of cards a 2 dimensional array? What is the second element doing for you?

    A deck of cards only needs to be a single array... deck[52];
    You initialize it like this...
    Code:
    for (int i=0; i < 52; i++)
      deck[i] = i;
    Then to shuffle it...
    Code:
    for (int x = 52; x > 0 ; x--)
       { y = rand() % x;   
         temp = deck[x];  
         deck[x] = deck[y];
         deck[y] = temp; }
    Don't forget to #include <time.h> and use ... srand(time(NULL)); ... exactly once at the top of your program, otherwise you will always get the same sequence.

    Ok... now you have this wildly out of order sequence of numbers from 0 to 51... how do you know which is which? Mathematically...
    Code:
    char suits[4][9]= {"hearts","spades","clubs","diamonds"};   
    char faces[13][6]= {"ace","two","three","four","five","six","seven","eight","nine", "ten","jack","queen","king"}; 
    
    int suit, face;
    
    // card is the value being examined
    suit = deck[card] % 4;
    face = deck[card] / 13;
    
    // to display it
    printf("card #%i = %s of %s\n", card, faces[face], suits[suit]);
    To deal a single card...
    Code:
    int deal = 0;
    
    // shuffle the deck...
    
    card = deck[deal++];
    ... where deal is a high water mark for the next card off the deck.


    Really guys .... it's not that hard. Both of you could probably do your projects in half the number of lines you're actually using.
    Last edited by CommonTater; 10-16-2011 at 02:59 PM.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Quote Originally Posted by CommonTater View Post
    Ok guys ... why is your deck of cards a 2 dimensional array? What is the second element doing for you?
    I don't know why the professor wants us to use the deck of cards as a 2 dimensional array, but the assignment was to build upon the code he had given us, which was

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define NCARDS 52
    #define NPROPS 2
    #define NSUITS 4
    #define NFACES 13
    
    char* suit[NSUITS]= {"hearts","spades","clubs","diamonds"};
    char* face[NFACES]= {"ace","two","three","four","five","six","seven","eight","nine",
                         "ten","jack","queen","king"
                        };
    void PrintCard(int deck[NCARDS][NPROPS], int i);
    void InitDeck(int deck[NCARDS][NPROPS]);
    void SwapCards(int deck[NCARDS][NPROPS], int src, int dest);
    void ShuffleDeck(int deck[NCARDS][NPROPS]);
    int GetPlayValue(int deck[NCARDS][NPROPS], int i);
    
    int main()
    {
        int deck[NCARDS][NPROPS];
        int i;
        srand(time(NULL));
        InitDeck(deck);
        ShuffleDeck(deck);
        for (i=0; i<NCARDS; i++)
        {
            PrintCard(deck,i);
        }
        return 0;
    }

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You may want to ask him or her ...

    You can compensate by modifying my code snips, but I really don't see much point in it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shuffling a deck?
    By Neo1 in forum C++ Programming
    Replies: 23
    Last Post: 07-16-2007, 01:21 AM
  2. Replies: 2
    Last Post: 11-07-2003, 12:21 AM
  3. card shuffling dealing question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2002, 08:37 PM
  4. card shuffling array(help!!!!!!!!!)
    By TheWaRpedOnE in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2001, 01:25 PM