### 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 9 10 11 12 2 3 4 5 6 7 8 9 10 J Q K A card # 13 14 15 16 17 18 19 20 21 22 23 24 25 2 3 4 5 6 7 8 9 10 J Q K A card # 26 27 28 29 30 31 32 33 34 35 36 37 38 2 3 4 5 6 7 8 9 10 J Q K A card # 39 40 41 42 43 44 45 46 47 48 49 50 51 2 3 4 5 6 7 8 9 10 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:

• Write a function to shuffle the `deck` by initializing all its entries to false:

`void shuffle();`

• Write a function to deal a card from the deck. Use the random number generator to pick a number between 0 and 51, and check to see if that number card has already been dealt. If it has, then try again -- until a card is found that has not been dealt. The function should return that card number:

`int deal_one_card();`

• Write a function that deals a hand of thirteen cards to each player.

`void deal();`

• Write a function called `find_suit()` that returns the suit of a card, given its card number. The return value should be a single character: 'C' = clubs (), 'D' = diamonds (), 'H' = hearts (), 'S' = spades ().

• Write a function called `find_rank()` that returns the rank of a card, given its card number. The return value should be a single character: '2'..'9' = 2 through 9, '0' = 10, 'J' = jack, 'Q' = queen, 'K' = king, 'A' = ace.

• Write a function called `print_hand()` that prints out each player's hand, using `find_suit()` and `find_rank()` to determine the name of each card.

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.

• You MUST use BUBBLE SORT.

• You MUST pass the array being sorted as an argument to the `sort_hand()` function.

• You MUST use the `swap()` function.

• You MUST print out all four hands BOTH BEFORE AND AFTER you have sorted them.

• For reference, look at sections 6.6 and 7.6 in the textbook.

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.

• You MUST use BINARY SEARCH.

• You MUST use RECURSION.

• You MUST print out the number of the player who has the queen of spades.

• For reference, look at sections 6.8 and 5.13 in the textbook.