Sort Cards

  1. Extend your previous program for printing card. Define a function print_cards(int card[], int n) which will invoke print_card() n times to print n cards.
  2. 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.
  3. 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.
  4. 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
    
    
  5. 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;
                }
    }
    
    
  6. This homework requires you to design two functions to compare the values of two cards, so that the following main program can work correctly:
    
    void get_cards(int card[], int n)
    {
        for (int i=0; i<n; i++)
            card[i] = rand() % 52;
    }
    
    int main()
    {
        int seed;
        const int N = 13;
        int my_card[N];
        cout << "Please input a random seed -- ";
        cin >> seed;
        cout << endl;
        srand( seed );
        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;
    }
    
    
  7. 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.