CSE 131 Module 3: Arrays


Before beginning any work, do a Team...Pull in your repository.


Dice Simulation

In this lab you will simulate the throw of a number of Dice. This will require using what you have learned in Module 2 and Module 1 but will also require the use of arrays as taught in this module.

We ask that you follow these instructions carefully, step-by-step, so as to develop your code by concept refinement. Each concept is first described, and then your group takes steps to implement the concept.


In the steps described below, pictures are shown of dice to illustrate the concepts involved in this lab. You will not really be using or displaying the pictures. Instead, your program will simulate throwing dice and you will measure the properties of interest.
  1. You need two pieces of information from the user:
    How many dice will be used in this simulation?
    For the purposes of this document, let us say that D dice are used.

    As an example, if D is 3, then a given throw could be:

    How many times will the dice be thrown?
    For the purposes of this document, let us say that the dice will be thrown T times.

    As an example, if T is 5, then the dice could be thrown as follows:

    Figure 1
    At this point,
    • Create a class Dice in the lab3 package (which is in the labs source folder).
    • Decide on meaningful variable names for the concepts introduced so far:
      • The number of dice used in this simulation.
      • The number of times the dice are thrown.
    • Insert code to obtain the values for these variables from the user.
  2. Now let's simulate the dice rolls.
    For the purposes of this lab, let's say that each die is six-sided, and therefore labeled to show the values 1 through 6. If you have time, you might consider how to change your code to accommodate dice that have fewer or more than 6 sides.
    Iteratively assign all the dice random values.
    Spoilers: You will need to sum the values shown on the dice. You should therefore pick a data type that makes this easy.
  3. You next need a mechanism for remembering the value of each die as it is thrown.

    Consider the example shown in Figure 1. After the first throw, we have:

    You must provision an array with one slot per die to hold the results of each throw. For the example above, your array might look like:
    array index 0 1 2
    array contents 3 5 3

  4. Insert code to print the array's values, and run your program several times to make sure the values look reasonable.
  5. Around the code you have currently programmed, insert a loop so that your code iterates T times.
    Recall T was a value prompted from the user. It is the number of times we will throw the dice.

    You do not need a different or larger array than the one you already have. You only need to remember the dice values after each throw. You will not be required to remember the values from all of the throws.

  6. Run your program and examine the values printed from the T iterations. They should continue to look reasonable (albeit random).
  7. One thing we are interested in is how often all of the dice show the same value after they are thrown within one of the T iterations. In the example shown in Figure 1, this happened 1 in 5 times (so, 20% or 0.2).

    Add code to your program so that it computes and prints the fraction of times that all of the dice have the same value in a throw. This requires:

  8. We are next are interested in the sum of the dice values for each throw. Arrange for your program to print out the dice values' sum each time the dice are thrown and make sure it is working correctly.
  9. Finally, we are interested in how often each possible sum occurs as the dice are thrown. In the example in Figure 1, the sums were seen as follows:
    Sum Number of times
    Sum was seen
    7 1
    8 2
    11 1
    18 1
    Some things to keep in mind:
    • What values could the sum possibly take, given that there are D dice?
    • Based on what we have studied thus far, what type of variable is needed to keep track of the sums?
    • Where in your code should you update how often a sum occurs?
    Arrange for your program to produce (print) a table such as the one shown above. If you would like columns to line up, then note that when you print output, the String "\t" produces a tab character.
  10. Arrange for your program to produce output (formatted as you like) that shows how often each sum occurs as a fraction or percentage of all of the throws. Do not report 0 for sums that did not occur.
    For D dice, which sums occur most frequently? You can determine this by running your program on different numbers of dice, but using enough iterations to show typical behavior.

Submitting your work (read carefully)

Last modified 01:52:17 CDT 21 October 2018
When you are done with this lab, you must be cleared by the TA to receive credit.

This demo box is for lab 3
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:

TAs double check!
  • This demo box is for lab 3
  • The student has committed and pushed the work, and verified that it appears at bitbucket.
TA: Password: