CS 205 ­ Programming for the Sciences Spring 2011 ­ Programming Assignment 5 20 points Out: February 18, 2011

advertisement
CS 205 ­ Programming for the Sciences
Spring 2011 ­ Programming Assignment 5
20 points
Out: February 18, 2011
Due: February 23, 2011 (Wednesday)
The purpose of this assignment is to continue practicing creating C# programs using MS Visual Studio. Please name the project files and program files exactly as given in the assignment, and please write your name in a comment at the top of the program file. Problem Statement
In games using a pair of dice, it is important to know the probability of rolling a particular value, especially when betting on the outcome as in the game of Craps. We will simulate the rolling of a pair of dice using a pseudorandom number generator. The program should ask the user for the number of dice rolls to be simulated, do the simulation, then display the recorded distribution and the average roll value.
The program output might look like:
Enter the number of dice rolls to simulate: 10000
The distribution of roll values is:
2: 269
3: 559
4: 861
5: 1055
6: 1375
7: 1703
8: 1355
9: 1122
10: 853
11: 582
12: 266
The average roll value is 7.0167
Assignment
Create a C# console project named DiceSimulationProgram. Rename file "Program.cs" to "DiceSimulation.cs" (and the class "Program" to "DiceSimulation").
Write a C# program that implements a solution to this problem. We will keep track of the simulated roll values in an array of integers by using an array element as the counter of the roll values equal to its index. This program must meet the following specifications:
It must declare a static class variable of Random type and create a Random object. (Same as in the in­class exercise RandomNumbersProgram.)
● It must define a method ThrowDice. This method uses the Random object to compute two random integers between 1 and 6 (i.e., the values of two simulated thrown dice). Remember that the upper ●
02/17/2011
Page 1 of 2
D. Hwang
bound of the Next method is open. It passes back this data to the method caller. This method does not return a result.
● It must define a method SimulateDiceThrows that receives an array of integers and the number of throws to be simulated. This method simulates the throws and keeps track of the throws by incrementing the array element with an index of the roll value. This method does not return a result. Here is some pseudocode for this method:
1. Use a loop to count from 1 to the number of throws to be simulated
1.1. Call the ThrowDice method to get two random integers between 1 and 6 to simulate a throw of a pair of dice
1.2. Sum the two integers to get the roll value
1.3. Increment the element of the array that has the roll value as its index
It must define a method DisplayDistribution that receives an array of integers. This method displays the distribution of the simulated rolls using a loop to access the array elements. Since roll values start at 2, the loop counter should start at 2. (What number should the loop counter stop at?) The roll value should be in a field width of 3 and the number of the roll values should be in a field width of 5. This method does not return a value.
● It must define a method ComputeAverageRollValue that receives an array of integers and the number of simulated throws and returns the average roll value. This will be similar to the average computing methods we have seen. The difference is that each element of the array is the number of rolls with its index value, so the element must be multiplied by the roll value it represents (i.e., its index) before adding it to the sum. Then the sum is divided by the number of throws. Note the loop should count the same as in DisplayDistribution.
● To complete this assignment, the Main method must solve this problem in the following way.
●
1. Declare and create an integer array of size 13. (Why 13?)
2. Ask the user for the number of throws to simulate
3. Call the SimulateDiceThrows method with the array and the number of throws to simulate the dice throws
4. Call the DisplayDistribution method with the array to display a table of the distribution of roll values
5. Call the ComputeAverageRollValue with the array and the number of simulated throws to compute the average roll value
6. Display the average roll value with a precision of 4
An interesting thing about simulating dice throws is that since the probability of each number on a die is equal (1/6), the probability of the roll value is simply the number of possible combinations that result in that roll value over the possible number of combinations (6x6=36). For example, the probability of rolling a 7 is 6 out of 36 possible combinations, so we would expect 7 to be about 1/6 (0.167) of the total number of throws simulated. In the example run above, we got 1703/10000 (0.170) which is pretty close. Also, since the combinations are symmetric around 7, we would expect the average roll value to be around 7.
What to turn in
Make sure your name is in a comment at the top of the program file. Create a zipfile of your entire DiceSimulationProgram project folder and submit it using the submission system as explained in the handout Submission Instructions for CS 205.
02/17/2011
Page 2 of 2
D. Hwang
Download