Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CIS 111 C Programming Programming Assignment Two Die Roll Probability Simulator Overview In this assignment, the student will write a program that simulates the rolling

CIS 111 C Programming Programming Assignment

Two Die Roll Probability Simulator

Overview

In this assignment, the student will write a program that simulates the rolling of dice and collects statistical distribution data.

When completing this assignment, the student should demonstrate mastery of the following concepts:

Functions

Pseudo-Random Number Generation

Random Ranges

Statistical Distributions

Empirical Data Simulation

Assignment

Write a program that simulates the rolling of two dice. Assume that both dice contain the same number of pips. The two dice are rolled together at the same time and the value of the roll is calculated as the sum of the number of pips appearing on both dice (i.e. if we are rolling 6-sided dice and one die contains a 4 and the other contains a 3, a value of 7 is recorded for the roll). The program should make use of three #define statements containing information about the smallest number of pips on a given die and the largest number of pips on a given side of the die (assume the dice are fair and all values in between are equally accounted for the die will produce a uniform probability distribution), and the number of times that the pair of dice are thrown (trials). The program should begin by initializing an array of integers to contain all zero values. As the simulation commences, the appropriate element of the array is incremented to keep a tally of the number of times each roll is produced. After the simulation is complete, a small table is printed that contains all possible roll values and the frequency that each value appeared in the simulation. You are provided the genRand() function that produces random results between the two argument values (inclusive). You are to provide an implementation for getTwoDieSum(), which should make use of the genRand() function to produce two die rolls. The result of a roll should be calculated in getTwoDieSum() and returned to the driver function through a return value. In the driver, you must seed the random number generator, and write code that makes the appropriate number of calls to getTwoDieSum() to populate the array. The driver function should also display the table in a neat, organized fashion. You may put the table display code in an additional function if you wish. Please begin you program with the following shell:

#include #include ">

Example 1:

Example 2:

Solution:

// ----------------------------------------------------------------------------

// INCLUDES

#include

#include

#include

// DEFINES

#define LOWER_DIE 1 // LOWER_DIE must be at least 1

#define UPPER_DIE 10

#define TRIALS 10000

// PROTOTYPES

int genRand(int, int);

int getTwoDieSum();

// MAIN

int main() {

int numberArray[2 * UPPER_DIE + 1]; // holds frequency of rolls

// STEP 1 - Write a loop that goes through 'numberArray' and initializes the value

// in each array element to zero. Recall, the purpose of this array is to hold a tally

// count for each occurance of a certain roll coming up during the later simulation.

// After you write a loop that goes through each element and initializes it to zero,

// write another loop that iterates through each elements and displays the value on

// the screen to confirm that the zero values have been properly loaded. When you are

// satisfied that your initialization code is working properly, you can delete the test.

// INITIALIZE 'numberArray' HERE...

// STEP 2 - Seed the random number generator next. Remember that the placement and manner

// in which the srand() call is made is very important. You should only seed the random

// number generator one time towards the beginning of your program. It would not be appropriate

// to put the seeding process in a function or any other place in the program where there is

// a chance of the call being made multiple times; this can result in your program pulling

// the same value over and over again

// SEED THE RANDOM NUMBER GENERATOR HERE...

// STEP 3 - PART A - At this point in the program, you will need to write a loop that simulates

// the rolling of the two dice. This task will be accomplished by using the provided functions

// (genRand(), and getTwoDieSum()) to generator random numbers that represent the rolling process.

// Before the simulation portion of the program can be written here, these functions must be completed

// and tested.

// STEP 4 - Now that genRand() and getTwoDieSum() have been written, roll the dice and keep track of

// what was rolled in 'numbersArray[]'. The way the indexes are being setup is creating a situation

// where each unique roll possibility corresponds to an index in 'numbersArray[]' For example, if a

// call to 'twoDieSum()' results in a random roll of value 5 to be generated, we should modify the

// counter at index 5 in the array to go up by one (the value in numbersArray[5] is incremented).

// To simulate the complete rolling process, a loop that runs TRIALS number of times will have to

// be created. Inside of this loop, you will have to go through the process of calling 'getTwoDieSum()'

// to get a result index and then increment the appropriate index in 'numbersArray[]'. After going through

// this process TRIALS times, the results of the experiment will have been recorded and we can move onto

// displaying the results.

// WRITE A LOOP THAT PERFORMS THE ROLLS AND STORES THE RESULTS HERE...

// STEP 5 - At this point the experiment has been completed, but the results have not been shown on the screen.

// Another loop will have to be written that iterates through the indexes in 'numbersArray' that contains the

// results of this particular experiment. Consider which indexes contain information that we care about; not all

// of them will be appropriate to show on the screen. For example, if we are simulating the rolling of 6-sided

// dice, the lowest possible result value would be 2 and the highest possible result value would be 12. It would

// not be appropriate to show the value stored in 'numbersArray[0]' or 'numbersArray[1]' since these rolls could

// never occur. After you have a loop setup that goes through all of the possible relevant result index values,

// write a series of display statements that neatly draw a table on the screen summarizing the results.

// DISPLAY THE RESULTS FROM THE EXPERIMENT IN A TABLE HERE...

// STEP 6 - Run and test your program multiple times using different values for LOWER_DIE, UPPER_DIE, and TRIALS.

// Check the results for sanity and ensure only values in the appropriate ranges are being created. Check the

// collective results and ensure that a statistical distribution that is close to normal is being produced.

return 0;

}

// FUNCTION IMPLEMENTATIONS

int genRand(int lower, int upper) {

// STEP 3 - PART B - The 'genRand()' function has already been written for you in this assignment.

// At this point you should take a look at the code inside of this function until you have a firm

// understanding of the manner in which is generated a random integer between lower and upper. Develop

// a firm sense of the lowest and highest possible values that could be generated based on the values

// provided for 'lower' and 'upper'. To confirm your understanding, go back to main() and write loop

// that calls this function multiple times. Call 'genRand()' by passing it the values indicated in

// LOWER_DIE, and UPPER_DIE and try modifying the values in these constants to ensure the responses

// are consistent with the type of die being described by these constant values (i.e. 6-sided,

// 10-sided, 20-sided, etc...)

int range = (upper - lower) + 1;

return rand() % range + lower;

}

int getTwoDieSum() {

// STEP 3 - PART C - Once you have a firm understanding of the way that 'genRand()' is working, you are

// ready to provide an implementation for 'getTwoDieSum()'. 'genRand()' only generates a single random

// number and we will need to generate 2 unique random numbers to simulate the rolling of two of our dice.

// In 'getTwoDieSum()', you should make two different calls to 'genRand()', add the results together and store

// the value in a variable. After you have done this, you can then return that sum value to be handled

// by 'main()'.

// YOUR IMPLEMENTATION GOES HERE

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions