
Homework 3 — Arrays and Pointers
Due: Sunday, November 23, 2008, at 11:59pm
Gather and analyze statistics about the game of craps.
After successfully completing this assignment, you should be able to:–
· Create arrays of data
· Pass arrays as arguments to functions
· Allow functions to modify the contents of arrays
· Use pointer arithmetic for arrays
Read Chapter 5 of The C Programming Language, 2nd edition, by Kernighan and Ritchie. To better understand the Bubble Sort algorithm, consult Wikipedia or other resources.
Create a new program called hw3.c:–
· Copy and modify the function playGame from Homework #2 so that (a) it does not print anything, and (b) it returns an integer reflecting both the number of throws of the dice and whether the game was won or lost. In particular, if the player wins a game after n throws, it should return the integer value n. If the player loses after n throws, it should return the integer value –n.
· Create a new function that prompts the user for the number of games of craps to play and then plays that number of games without human intervention (i.e., by calling playGame). Store the results of the games in an appropriately-sized array, one element per game. In particular, the number of elements in the array should be exactly the number of games entered by the user.
· Print out the number of games played and compute and the mean and median numbers of throws required for a game to be won or lost.
· From the array of results, print out the answers to the following questions:–
1. What is the probability that the player wins a game of craps?
2. How many games are won on the first throw, second throw, third throw, …, twentieth throw, and after the twentieth throw?
3. How many games are lost on the first throw, second throw, third throw, …, twentieth throw, and after the twentieth throw?
4. Do the chances of winning improve with the length of the game?
In particular, for questions 2 and 3, print out a table showing the number of throws and for each number of throws, the percentages of games with that number of throws that are won and lost. You will need to construct another array to generate this data.
Test and debug your program with at least 1000 games. After you have debugged your program, modify it to seed the random number generator with the time of day and test it again. Be sure that the program you turn in is seeded with the time.
· stdio.h provides printf, scanf, getchar
· stdlib.h provides rand, srand, and abs
· time.h provides time, which is used to “seed” the random number generator
The mean number of throws is easy to calculate. Simply add up the number of throws in the array and divide by the number of games. Be sure you take the absolute value of the number of throws, so that losing games do not cancel winning ones.
To calculate the median, you must sort the array. If a sorted array A has n elements, the median is defined as A[n/2] if n is even and as (A[n/2] + A[n/2+1])/2 if n is odd.
You may sort the array using the Bubble Sort algorithm, shown here:–
void bubbleSort (int A[], const int
arraySize) {
int i, j;
for(i = 0; i < arraySize; i++)
for(j
= 0; j < arraySize-1; j++)
if(abs(A[j]) > abs(A[j+1]))
swap(A+j,
A+j+1);
} // void
bubbleSort(…)
Note that this algorithm sorts the array A in place. Note also that it sorts by the absolute values of the elements of A. Finally, note that it uses array arithmetic to pass pointers to the swap function.
The swap function is
void swap (int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
} // void
swap(…)
You should write a short text file (i.e., .txt format) called README, to be submitted with your program. Submit your implementation and README files using the following turnin command:–
/cs/bin/turnin submit cs2301 hw3
hw3.c README
Be sure to put your name at the top of ALL files! You would be surprised at how many students forget this.
Programs submitted after 11:59pm on November 23 will be tagged as late, and will be subject to the late homework policy.
This assignment will be graded on the following areas: documentation (including pre- and post-conditions for all functions, specified as comments in the code), proper choice of parameters and local variables, proper choice of function return types, adherence to specifications for each function, correctness, and robustness (ability to recover from invalid input). Programs must compile successfully in order to receive points for correctness.