Replacing arrays in order to print the full names of cards from a deck

This is a discussion on Replacing arrays in order to print the full names of cards from a deck within the C Programming forums, part of the General Programming Boards category; I am studying for my final and we just barely had time to cover pointers and strings, so I'm having ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    Replacing arrays in order to print the full names of cards from a deck

    I am studying for my final and we just barely had time to cover pointers and strings, so I'm having some trouble getting the hang of things.
    In the following code, I'm supposed to modify the program so that it prints the full names of the cards that you are dealt (e.g. Eight of clubs). It suggests that I do this by replacing rank_code and suit_code by arrays containing pointers to strings.

    Code:
    #include <stdbool.h> 
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define NUM_SUITS 4
    #define NUM_RANKS 13
    
    int main(void)
    {
          bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
          int num_cards, rank, suit;
     
         const char rank_code[] = {'2','3','4','5','6','7','8',
                                 '9','t','j','q','k','a'};
         const char suit_code[] = {'c','d','h','s'};
      
         srand((unsigned) time(NULL));
      
         printf("Enter number of cards in hand: ");
         scanf("%d", &num_cards);
      
         printf("Your hand:");
         while (num_cards > 0)  {
              suit = rand() % NUM_SUITS;    /picks a random suit */
              rank = rand() % NUM_RANKS;    /picks a random rank */
            if (!in_hand[suit][rank])  {
                in_hand[suit][rank] = true;
                num_cards--;
          printf(" %c%c", rank_code[rank], suit_code[suit]);
        }
       }
       printf("\n");
       
       return 0;
    }
    From what little I understand, I need to use a pointer array for rank_code similar to

    Code:
    const char *rank_code[] = {"Two", "Three", "Four", "Five", "Six", 
           "Seven", "Eight", "Nine", "Ten", "Jack", 
           "Queen", "King", "Ace"};
    Am I on the right track or is there a lot more to this than I understand?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, right track, but I'd move the "Two", up to rank_code[2], "Three" to rank_code[3], etc. Yes, it wastes rank_code[0] and [1], but it works very transparently, imo.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    So would I just leave the first two ranks empty, by starting the array off like this? :
    Code:
    const char *rank_code[] = {"", "", "Two", "Three", "Four"};
    Do I need to manipulate the code elsewhere after I substitute the rank_code and suit_code arrays in that fashion?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Bibble View Post
    So would I just leave the first two ranks empty, by starting the array off like this? :
    Code:
    const char *rank_code[] = {"", "", "Two", "Three", "Four"};
    Do I need to manipulate the code elsewhere after I substitute the rank_code and suit_code arrays in that fashion?
    Sure. You no longer want a random number less than 2, so set up your random to be +2. Your define of num ranks needs to be moved up by 2 as well.

    Things like that. Your program is nice and short, and you should have no problems finding any changes that need to be made, with some testing.

    I'd also be tempted to put in a "zero" and a "one" string in the ranks, not because you'll ever use them (hopefully), but because seeing a word that you should never see, is a great bug indicator. Just seeing a nothing string doesn't tell you much of anything. This is only a helper to find bugs, but that's good to have!

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Nevermind, sorry, I fixed it with a couple of well-placed \n's. Thanks for the help!
    Last edited by Bibble; 04-30-2012 at 09:56 PM. Reason: found my issue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to Model a Deck of Cards.
    By _lisa in forum C Programming
    Replies: 3
    Last Post: 11-15-2011, 11:05 PM
  2. Trying to make a deck of cards.
    By Jesse20ghet in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2011, 02:44 PM
  3. How do you assign Full names to a array string
    By Demipimp in forum C Programming
    Replies: 9
    Last Post: 12-05-2008, 11:56 AM
  4. filling a deck of cards
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 12-07-2002, 09:13 AM
  5. Names into Alphabetic Order
    By |PiD| in forum C Programming
    Replies: 5
    Last Post: 11-20-2002, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21