Thread: Help me understand this program

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Help me understand this program

    Hi all,

    My teacher used the following example to teach us about multiple-dimension arrays:
    Code:
    /* Deals a random hand of cards */
    
    #include <stdbool.h>   /* C99 only */
    #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;
    }
    The part that I don't understand is this:
    Code:
    suit = rand() % NUM_SUITS;
    rank = rand() % NUM_RANKS;
    There are 4 suits, ranging from 0 to 3, and 13 ranks, ranging from 0 to 12. The value returned by the statements above can only return 0 to 9; doesn't that mean it's going to leave out some ranks, and possibility going out of bound for suits?

    Obviously I'm new to C (and programming in general), and I've only gotten to arrays, so please take that into consideration when you explain things.

    Much thanks!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why do you think that can only return 0-9?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    claudiu,

    Lol, I see how it works now. I was thinking about a large number generated by rand() divided by a variable.
    Wow, I feel so stupid now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. don't understand an array in function program
    By ashlee in forum C Programming
    Replies: 3
    Last Post: 12-01-2010, 10:55 PM
  2. Replies: 4
    Last Post: 08-18-2009, 03:32 PM
  3. program that i couldn't understand
    By elton_fan in forum C Programming
    Replies: 9
    Last Post: 03-24-2007, 03:18 PM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM