Computer Science I
MC140.01 / MC140.02
Fall 2000
Assignment #6
due Wednesday 15 November 2000

The goal of this assignment is to learn about sorting and searching arrays and to practise using pointers.

The assignment is worth 5% of your term mark. It will be marked out of 50 points. This assignment uses functions from assignment #5. 5 points are allotted to all the functions from assignment #5. 40 points are allotted to each of the new functions described below. 5 points are allotted to comments and general readability of your code. Make sure you have a header comment at the top of your program, as well as comments at the beginning of each function. Make sure your code is nicely indented and your variables have meaningful names.

Look on the web page for a skeleton file for this assignment (ass6-skeleton.c).

As usual, submit your program electronically. So you should deposit your file in the professor's folder, named as follows: sklarel-ass6.c. Bring a hardcopy to class. Please staple loose sheets together.

In this assignment, you will write a program to deal four hands for the game of hearts from a deck of cards, sort each hand and determine which player has the queen of spades. In the classic game of hearts, there are four players and the entire deck is dealt out so that each player gets 13 cards. The queen of spades is an evil card, so you always want to know who has it.

In this assignment, you will handle the deck of cards and the players' hands just as you did with assignment #5.

Use a global, one-dimensional array with 52 entries to keep track of the cards that have been dealt (to make sure you don't deal the same card twice). This is an array of flags, where a flag is a logical variable that can be either true or false (1 or 0) -- if an entry is true (1), it means that card has been dealt; if an entry is false (0), then that card has not been dealt. Declare this array as follows:

     int deck[52];

In the deck array, the cards are numbered from 0 to 51, and you can think of each number as in index into a table like this:

card # 0 1 2 3 4 5 6 7 8 9101112
2 3 4 5 6 7 8 910 J Q K A
card # 13141516171819202122232425
2 3 4 5 6 7 8 910 J Q K A
card # 26272829303132333435363738
2 3 4 5 6 7 8 910 J Q K A
card # 39404142434445464748495051
2 3 4 5 6 7 8 910 J Q K A

Use four global, one-dimensional arrays with 13 entries to keep track of each player's hand, declared as follows:

     int player1[13], player2[13], player3[13], player4[13];

1. (5 points) Use the following functions from assignment #5, modified to accommodate 4 players and 13 cards per player:

2. (20 points, 5 for swap, 15 for sort_hand) Write two functions called swap and sort_hand(), that implement bubble sort to sort each hand in order of suit and then rank. This is actually the numeric order of the card indices, as shown in the table (above), so you don't need to worry about the suits and ranks -- just sort the entries in each player array.

3. (20 points) Write a function called search_hand() which uses BINARY SEARCH and RECURSION to determine which player is holding the queen of spades. This card has index "49" in the table, so you have to look for "49" in each player's hand. Print out the number of the player who has the queen of spades in his hand.