playGame
and rollDice
from
Homework 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, playGame
needs to initialize (i.e., "seed") the random number generator.)
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.
.c
files, create the following
functions:
playGame
). It stores 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.
main()
function, that controls all of the others. In particular, main
or one of the functions it calls must free any arrays that were allocated.
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
.
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.
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.
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.
make
, and
correct operation of make
for individual files and for
clean
- 5 points
.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
.