Homework 4
User-defined functions: Modifying the craps game program

Due: Tuesday, November 20, 2012 at 5pm

Outcomes

After successfully completing this assignment, you will be able to...

Before Starting

Read Chapter 6 and 7 of Deitel & Deitel, and do Lab 4.

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. For the analysis portion of this assignment, your program needs to print out answers to the following questions:
  1. What is the probablility that the player wins a game of craps?

  2. 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.)

  3. 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%.)

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

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 web-based turnin. Programs submitted after 5pm on November 20 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.