Homework 4
User-defined functions: Modifying the craps game program
Due: Tuesday, November 22, 2011 at 5pm
Outcomes
After successfully completing this assignment, you will
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
- Build a multi-file application in C
- Use makefiles to correctly build, rebuild, and clean your working directory
Before Starting
Read Chapter 5 of Kernighan and Ritchie.
The Assignment
In this assignment you will gather and analyze statistics about the game
of craps. Your program will be based on multiple C source files and
header files, and a makefile.
-
Copy the functions
playGame and rollDice from Programming Assignment #3 into a new file named, for example, playcraps.c. Create a new header file, playcraps.h, that contains the function prototype of playGame and anything else needed by a caller of playGame for example, to initialize (i.e., "seed") the random number generator.
- Modify
playGame 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.
- In one or more new
.c files, create the following
functions:
- A 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). It stores the results of the games in an array, one element per game.
- A function to print out the number of games played and compute the
mean and median numbers of throws required for a game to be won or lost.
- A function to analyze the array of results and print out answers to the questions listed below.
- The
main() function, that controls all of the others.
- Construct a
makefile patterned after Lab #4 to build the executable file (call it statistics) and all of its components. Make sure that $(CFLAGS) is specified in the commands to build individual modules and that it defaults to -g. Be sure that your makefile rebuilds only those files that are necessary and that it can make your directory clean.
For the analysis portion of this assignment, your program needs to print
out answers to the following questions:
- What is the probablility that the player wins a game of craps?
- What percentage of games are won on the first throw, second throw, third throw, ..., twentieth throw, and after the twentieth throw? (The sum of these percentages should total to the probability in Question 1, expressed as a percentage.)
- What percentage of games are lost on the first throw, second throw, third throw, ..., twentieth throw, and after the twentieth throw? (The sum of these percentages, when added to the probability in Question 1 expressed as a percentage, should total 100%.)
- 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 may 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.
Include files
- stdio.h provides printf(), scanf(), getchar()
- stdlib.h provides rand(), srand(), and abs()
- time.h provides time()
Means and Medians
Means and medians are commonly used in engineering and scientific calculations. 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 odd and as (A[n/2-1] + A[n/2])/2 if n is even. (This particular definition of the median is based on the fact that arrays in C are indexed from zero.)
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);
}
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;
}
It is best to put the bubbleSort and swap functions in its own .c file called bubblesort.c. To make bubbleSort available, there should also be a bubblesort.h file. Note that the swap function should not be mentioned in bubblesort.h, because that is strictly internal to the implementation of bubble sort and won't be called independently by any other program.
Deliverables
Write a short file called README.txt that summarizes your program,
how to run it, and any problems that you encountered. Zip together all
of your .c and .h files, your makefile, and your README.txt file into
a file named project4.zip and submit your file
using the following
turnin command:
/cs/bin/turnin submit cs2301 PROJECT4 project4.zip
Programs submitted after 5pm on November 22 will be tagged as late,
and will be subject to the late homework
policy.
Grading
This assignment is worth 30 points.
Programs must compile successfully in order to receive any credit.
It is suggested that before you submit your program, compile it again on a
CCC system to be sure that it compiles correctly.
- Correct compilation without warnings using
make, and
correct operation of make for individual files and for
clean - 5 points
- README.txt submitted as specified - 2 points
- Program documentation and formatting, including pre- and post-conditions
for each function - 3 points
- Program organization into three or more
.c files, two
or more .h files, and a makefile - 5 points
Note: The .c files should include at least playgame.c (or whatever you
wish to call it), the new .c file that sets up the array and plays the
requested number of games, and your sort function. The final target of the
makefile should be an executable called statistics.
- Correctly playing the
game n times, and correctly storing the results in the array - 5 points
- Correctly sorting the array and obtaining the mean and median number of
throws - 5 points
- Building a table or other suitable data structure to answer the four
questions, printing out those results, and getting answers consistent
with the TAs' test cases - 5 points