Thread: Sorting and help with arrays

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Sorting and help with arrays

    I have a program due that is to take 5 random cards from a deck, declare what suit they are and give the number of the card. Ul;timately what i need to do after this is to have them filter the cards into like numerical order.... basically put them lowest to highest ( from top to bottom) I got the code for this worked out but I am unsure how to get it to display H,C,S, or D for the suits. I got it to do a random number 0-4 for that display but I cant get it to display H C S or D. Also I am unable to get it to sort the array. I am unsure of the code and I know its really simple for all you people who do it all the time but its a basic beginner course and im having a bit of a hard time. Ill paste the code below. Any help is greatly appreciated

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int iValue, iSuit;
        int iLoop, iLoop2;
    
        float position[3];
    
        bool bCards[4][13];
    
        // Initializing the array
        // aka, putting all of the cards in the deck
        for(iLoop = 0; iLoop < 4; iLoop++)
            for(iLoop2 = 0; iLoop2 < 13; iLoop2++)
                bCards[iLoop][iLoop2] = 1;
    
        // Seeding the random number generator
        srand(time(NULL));
    
        printf("\n\n");
        printf("Suit      Value");
        printf("\n");
        printf("----      -----");
        printf("\n");
    
        for(iLoop = 0; iLoop < 5; iLoop++)
         {
            iSuit = rand()%4;
            iValue = rand()%13;
    
            while(bCards[iSuit][iValue] == 0)
            {
                iSuit = rand()%4;
                iValue = rand()%13;
            }
    
            printf("  %2d       %2d", iSuit, iValue);
            printf("\n");
    
            // Removing the card from the deck
            bCards[iSuit][iValue] = 0;
          }
          system("PAUSE");
          return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can make this really easy if you just use a single character for a card, and do a bit of math to find out what card it is:

    Use the numbers 1 - 52 to represent the cards.

    N / 13 gives you the suit of the card.
    N % 13 gives you the card number.

    Let's examine:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        const char *suits[] = { "Spades", "Hearts", "Clubs", "Diamonds", NULL };
        int x;
    
        for( x = 0; x < 52; x++ )
        {
            printf( "This card is the %d of %s.\n", x % 13, suits[ x / 13 ] );
        }
    
        return 0;
    }
    Now then, you'll get card numbers 0 through 12 for each suit. If you want, you can make another array which contains the card number or letter and do the same as I've done with the suit.
    Code:
        const char *faces[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    
    /*
    This card is the A of Spades.
    This card is the 2 of Spades.
    This card is the 3 of Spades.
    This card is the 4 of Spades.
    This card is the 5 of Spades.
    This card is the 6 of Spades.
    This card is the 7 of Spades.
    This card is the 8 of Spades.
    This card is the 9 of Spades.
    This card is the 10 of Spades.
    This card is the J of Spades.
    This card is the Q of Spades.
    This card is the K of Spades.
    This card is the A of Hearts.
    ...snip...
    This card is the K of Hearts.
    This card is the A of Clubs.
    ...snip...
    This card is the K of Clubs.
    This card is the A of Diamonds.
    ...snip...
    This card is the K of Diamonds.
    
    */
    In any event, when you do decide you need to actually sort the hand, you use any common sorting method, for example, qsort, or write a bubble sort or what not.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    ok I think i figured it ould actually got something acomplished but i may need one thing done.....

    Code:
     
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
      int i,j,k;
      bool cards[52];
      char *suits[]={"Heart   ",
                     "Club    ",
                     "Diamond ",
                     "Spade   "};
      char *faces[]={"Ace  ","1    ","2    ","3    ","4    ","5    ","6    ",
                     "7    ","8    ","9    ","10   ","Jack ","Queen","King "};
    
      // Initializing the array
      // aka, putting all of the cards in the deck
      for(i = 0; i < 52; i++) {
        cards[i] = 1;
      }
    
      // Seeding the random number generator
      srand(time(NULL));
    
      printf("Before Sorting\n\n"
             "  Suit      Value\n"
             "  -----     -----\n");
      for(i = 0; i < 5; i++) {
        do {
          j = rand()%52;
        } while(cards[j]==0);
        // Removing the card from the deck
        cards[j] = 0;
    
        printf("  %s  %s\n",suits[j/13],faces[j%13]);
      }
    
      // sort the cards
      printf("\nAfter Sorting\n\n"
             "  Suit      Value\n"
             "  -----     -----\n");
      for(i=0;i<13;++i) {
          for(j=0;j<4;++j) {
              k=i+j*13;
          if(cards[k]) {
      printf(" %s %s\n",suits[k/13],faces[k%13]);
                       }
                           }
                         }
    
      system("pause");
      return 0;
    }
    I just want it to sort the cards it already picked. The way it works, is that I think it prints out 47 cards and not the ones that were already picked. Do you know how I can fix this? Maybe there is just a little trick I need to do in the code to make it work?

  4. #4
    Professional Bit Wrangler
    Join Date
    Mar 2005
    Posts
    6
    Code:
        if(cards[k] == 0) {

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your sort is overly complex. You already know that the numbers will be from 0 to 51. You also know that numbers 0 through 12 are one set, 13 through 24 are another, and so on. Therefore, all you have to do is sort the entire set of 0 - 51 in one shot. There's no need to do anything more. Just do a simple sort. No need for the:
    Code:
    for(i=0;i<13;++i) {
          for(j=0;j<4;++j) {
    Just one sort numericly from zero to 51 gets it all done. Due to the way the card layout is designed, that automaticly sorts by suit and numberd order.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed