UMBC CMSC 104 * CSEE | 104 | current 104

Project 4

CMSC 104 -- Fall 1998

Project 4

Rolling Dice

Due Dates:

This project is due at Midnight, Sunday 12/13/98 for both sections


This project is designed to give you experience working with arrays and passing arrays to functions.

The program you write will simulate the rolling of a pair of dice 300 times. You will keep track of the number of times a particular dice face occurs (one through six) as well as the number of times each possible total (2 to 12) occurs. You will accumulate the counts in two separate arrays.

The probability of 1,2,3,4,5 or 6 showing on a die face are all equally likely, so we expect the total counts for each of these to be approximately the same. The possible totals of the two dice (2 to 12), however are not equally likely. The value 2, aka snake eyes, can only be produced when each of the two dice have come up 1's. Likewise, the value 12, aka boxcars, can only be produced when each of the two dice have come up with the value 6.

The other totals (3 to 11) will tend to occur more often than either the snake eyes or the boxcars, because there is more than one roll possible to create those values. The 3 could be gotten by die #1 rolling a 1, and die #2 rolling a 2 OR by die #1 rolling a 2 and die #2 rolling a 1. So theoretically, it is twice as likely that a 3 will be rolled, rather than a 2 or a 12. The total 7 is most likely to occur. Other totals rolled will exhibit other probabilities.

The basic project is to simulate the rolling of two dice using rand() and count how many times the dice show 1 - 6 AND how many times each of the possible totals (2 - 12) was rolled. The counts are to be stored in arrays. After all of the rolling is complete, print out results in the format shown below. DO NOT ATTEMPT TO STORE THE INDIVIDUAL VALUES ROLLED IN AN ARRAY. Your book can really help you out with this project, since it gives an example of a similar program. You are rolling two dice, rather than one.

Your program must include at least one function that takes the array as an argument. I would suggest the functions that do the printing as logical choices of such a functions.

You should do most of your work in functions. I would suggest that you write a function called RollDie, which would return the result of rolling 1 die. You should also have a function that will print out the number of times that each possible total occurred and a function that prints out the statistics on the number of times each die face (1 to 6) occurred. You may have other functions if you like. You output should be formatted so that the values and number of occurrences line up in columns as shown below.


Sample Output:

This sample output was generated using 44 as the seed of the random number generator. You can test your program by using 44 as the seed in your program. If you do so, your output will match this output exactly. The output on both umbc8 and umbc9 are identical to the output below.

NOTE -- your final version of this program should use a different seed for the random number generator each time it is run. This was discussed in class.

In 300 rolls of two dice: Die Occurrences Percentage 1 105 17.50 2 104 17.33 3 89 14.83 4 97 16.17 5 108 18.00 6 97 16.17 The sum of the dice were: 2 occurred 6 times: ****** 3 occurred 13 times: ************* 4 occurred 26 times: ************************** 5 occurred 41 times: ***************************************** 6 occurred 48 times: ************************************************ 7 occurred 43 times: ******************************************* 8 occurred 42 times: ****************************************** 9 occurred 37 times: ************************************* 10 occurred 22 times: ********************** 11 occurred 12 times: ************ 12 occurred 10 times: **********

15 Point Extra Credit Opportunity

As a bonus, add the following features to your program
  1. roll the dice a maximum of 30,000 times
  2. allow the user to specify the number of rolls
  3. use a scale when printing the historgram for the totals 2 - 12
Note to number 3 above --
If the user chooses to roll the dice 30,000 times, it is theoretically possible that the user will roll the same total 30,000 times. Since you obviously cannot print 30,000 *s on one line (the max is about 50), you need to create a scale for your histogram (i.e. each * represents some number of rolls). Print your histogram and your scale in a format similar to the following:
The sum of the dice were:
 2 occurred   61 times: ******
 3 occurred  139 times: *************
 4 occurred  264 times: **************************
 5 occurred  413 times: *****************************************
 6 occurred  480 times: ************************************************
 7 occurred  431 times: *******************************************
 8 occurred  423 times: ******************************************
 9 occurred  377 times: *************************************
10 occurred  227 times: **********************
11 occurred  129 times: ************
12 occurred  101 times: **********

            Scale: Each * equals approximately 10 rolls
You must absolutely do this extra credit with a function. You are likely to need more than one function.

If you do this extra credit portion, you must use the following number of rolls as the input from the user for your five test runs in the typescript file : 10, 100, 1000, 10000, 30000.


Submitting your project

After your program is coded and tested, run the following commands under the UNIX script facilty (assuming your UNIX prompt is %)
  % script
  % cc project4.c -lm
  % a.out
  % exit
  % mv typescript project4.out
Then submit your files (project4.c and project4.out) in the usual way.

For section 103, use the command -- submit cs104-103 proj4 project4.c project4.out

For section 301, use the command -- submit cs104-301 proj4 project4.c project4.out