Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(MUST BE DONE IN C) Write a program that simulates the rolling of two dice. Assu

ID: 3769899 • Letter: #

Question

(MUST BE DONE IN C) 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:

// INCLUDES

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// 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

// initialize numberArray

// seed the random number generator

// roll the dice and keep track of what was rolled in the array

// display results

return 0;

}

// FUNCTION IMPLEMENTATIONS

int genRand(int lower, int upper) {

int range = (upper - lower) + 1;

return rand() % range + lower;

}

int getTwoDieSum() {

// YOUR IMPLEMENTATION GOES HERE

}

SAMPLE OUTPUTS

Explanation / Answer

I was unable to look into the sample output images. Anyways, I developed a logic for you. Here is the code. If you have any further queries, just get back to me.

// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 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

// initialize numberArray
for(int i = 0; i < 2*UPPER_DIE+1; i++)
numberArray[i] = 0;

// seed the random number generator
srand(time(NULL));

// roll the dice and keep track of what was rolled in the array
for(int i = 0; i < TRIALS; i++)
numberArray[getTwoDieSum()]++;

// display results
for(int i = 2; i < 2*UPPER_DIE+1; i++)
printf("%2i occured %3i times. ", i, numberArray[i]);

return 0;
}

// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}

int getTwoDieSum() {
// YOUR IMPLEMENTATION GOES HERE
int die1 = genRand(LOWER_DIE, UPPER_DIE);
int die2 = genRand(LOWER_DIE, UPPER_DIE);
return die1 + die2;
}