Sort Cards

  1. Extend your previous program for printing cards. After randomly drawing 13 cards, sort them in the order when you play the contract bridge. That is, cards with same suits are sorted together.
  2. Then try to sort the cards again, with the order when you play the Big Deuce. In this order, C6 is higher than S5, so you should sort cards according to the ranks first.
  3. The output may look like below:
    
    SK S3 DK C8 H7 SJ C9 D9 DQ H5 HJ S7 H2
    ===== Sorted by bridge order =====
    C8 C9 D9 DQ DK H2 H5 H7 HJ S3 S7 SJ SK
    ===== Sorted by Big2 order =====
    S3 H5 H7 S7 C8 C9 D9 HJ SJ DQ DK SK H2
    
    
  4. Suppose we already have a function to perform the exchange sort:
    
    void exchange_sort(int a[], int n, int (*pcomp)(int, int))
    {
        int i, j, temp;
        for (i=0; i<n-1; i++)
            for (j=i+1; j<n; j++)
                if ( pcomp(a[i], a[j]) > 0 )
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
    }
    
    
  5. This homework requires you to design two functions to compare the values of two cards, so that the following main program can work correctly:
    
    int main()
    {
        int i;
        const int N = 13;
        int my_card[N];
        srand( time(NULL) );
        get_cards(my_card, N);
    
        print_cards(my_card, N);
    
        cout << "===== Sorted by bridge order =====\n";
        exchange_sort(my_card, N, bridge_comp);
        print_cards(my_card, N);
    
        cout << "===== Sorted by Big2 order =====\n";
        exchange_sort(my_card, N, big2_comp);
        print_cards(my_card, N);
    
        return 0;
    }
    
    
  6. You may have noticed that sometimes it will generate duplicate cards, which should not happen in a real card game. Theoretically, we should shuffle a deck of cards and draw 13 from them. However, shuffling is not the focus of this homework. Therefore, at this moment, let us still simply generate 13 random integers for this program.