CS1101C Practical Exam

Session 4 (1600 - 1745 hours)

Card Shuffling for Bridge

The name of your C program file must be called bridge.c, files with any other name will not be marked.

The deadline for this lab is Wednesday 12 April 2006, 17:45:59 hours. Strictly no submissions will be accepted after the deadline.

Background

A standard deck of 52 playing cards are used in most card games. Each playing card is unique and has a suit and a rank. There are four suits with their abbreviations as shown, and they are ranked from lowest (Club) to highest (Spade):

Suit Abbreviation
Club
C
Diamond
D
Heart
H
Spade
S

There are 13 suits with their abbreviations as shown, and they are ranked from lowest (2) to highest (Ace):

Rank Abbreviation
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
T
Jack
J
Queen
Q
King
K
Ace
A

Card

Each card is represented by its suit followed by its rank. For example, the Ace of Spades is also known as the Spade Ace, and is represented by SA. The Three of Diamonds is represented by D3. The Ten of Hearts is represented by HT.

Shuffling

After a deck of cards is randomly shuffled, we wish to distribute the entire deck of 52 cards equally among four players, with each player receiving 13 cards. We would like to show the cards obtained by each player; sorted first according to suit, then rank.

Examine the following sample output to see how the cards are sorted:

Player 1: C3 C8 C9 CK D6 DQ DK HQ S2 S3 S9 SJ SA
Player 2: C7 CJ CA D5 D7 H3 H7 H8 HJ HK S5 S7 SK
Player 3: C2 C4 C5 CT CQ D4 DJ H2 HT S4 S6 ST SQ
Player 4: C6 D2 D3 D8 D9 DT DA H4 H5 H6 H9 HA S8
Here is another sample output:
Player 1: C5 C6 D5 DJ DQ H2 H3 H5 HK HA S5 SQ SA
Player 2: C2 C4 C8 D6 D7 D9 DK H4 H7 H8 HT S2 S9
Player 3: C9 CJ CQ CK CA D2 D4 D8 DT H6 H9 HJ HQ
Player 4: C3 C7 CT D3 DA S3 S4 S6 S7 S8 ST SJ SK

Task

Write a program that randomly shuffles a deck of cards, distributes it to the four players, and displays the cards held by each player, sorted as shown above.

You are reminded to follow the sample output exactly; else marks will be deducted.

Do not use any structures or File I/O in your program, else no credit will be given.

Tips

You may wish to examine and make use of anything in the following program. It is called shuffle.c, and the program code can be found in your directory.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*  Declare function prototypes.  */
void shuffle(int a[], int n);
int rand_int(int a, int b);

int main(void)
{
    /*  Declare and initialize variables.  */
    /*  ...  */

    /*  Sets the seed to a random value based on the system time.  */
    /*  This is done only once, at the beginning of the program.   */
    srand((unsigned int)time(NULL));

    /*  Your code goes here...  */

    return 0;
}

/*---------------------------------------------------*/
/*  This function shuffles (mixes up) all the        */
/*  integers in the array.                           */
void shuffle(int a[], int n)
{
    int i, swapwith, temp;
    for (i = 0; i < n - 1; i++) {
        swapwith = rand_int(i, n - 1);
        if (swapwith > i) {
            temp = a[i];
            a[i] = a[swapwith];
            a[swapwith] = temp;
        }
    }
}

/*---------------------------------------------------*/
/*  This function generates a random integer         */
/*  between specified limits a and b (a<b).          */
int rand_int(int a, int b)
{
    return rand()%(b-a+1) + a;
}
/*---------------------------------------------------*/

You are reminded to follow the sample output exactly; else marks will be deducted.

Remember to submit your program using the submit bridge.c command, and check your submission using the check command.

All the best!

UNIX commands

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. dir”: lists all the files in the directory.
  2. cp a.txt b.txt”: copies a.txt to b.txt.
  3. mv a.txt b.txt”: moves / renames a.txt to b.txt.
  4. cat a.txt”: shows the contents of a.txt.