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

Write a C++ program, named DiceRoll.cpp, that contains a function named roll():

ID: 3736992 • Letter: W

Question

Write a C++ program, named DiceRoll.cpp, that contains a function named roll(): Return type: int B Name: roll Parameters: o int sides: number of sides in the die o double "weights: a pointer to an array of weight values for each side in the die weights[0]: weight value for side 1weights[1]: weight value for side 2 This function should simulate a roll of the weighted die, given its number of sides and weight values for each side. It should return a value between 1 and sides. In the main() function, test the roll) function by calling it 100,000 times and keeping track of the number of rolls for each side of a weighted coin flip: 2 Die with 2 sides (sum of all weights 20.5 2.5) side weight Probability 1 2 2 / 2.5-80% 2 0.5 0.5 / 2.5-20% Below are a few sample executions of the program. /* Execution Sample #1: 1 was rolled 80297 times. 2 was rolled 19703 times. Press any key to continue.."/ /* Execution Sample #2 1 was rolled 80095 times. 2 was rolled 19905 times. Press anv kev t o continue

Explanation / Answer

#include <iostream>

using namespace std;

int roll ( int n, double *weights ) {
double random = (rand()%1000000)/1000000.0;
for ( int i = 0 ; i < n ; i++ ) {
if ( random < weights[i] ) return i;
}
return n-1;
}

int main() {
int n;
cin >> n;
double weights[n];
for ( int i = 0 ; i < n ; i++ ) cin >> weights[i];
int count[n];
for ( int i = 0 ; i < n ; i++ ) count[i] = 0;
double sum = 0;
for ( int i = 0 ; i < n ; i++ ) sum += weights[i];
for ( int i = 0 ; i < n ; i++ ) weights[i] /= sum;
for ( int i = 1 ; i < n ; i++ ) weights[i] += weights[i-1];
for ( int i = 0 ; i < 100000 ; i++ ) {
count[roll(n,weights)]++;
}
for ( int i = 0 ; i < n ; i++ ) {
cout << i+1 << " was rolled " << count[i] << " times." << endl;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote