Please answer this assignment using C Thank you Overview Wri
Please answer this assignment using C++. Thank you!
Overview:
Write a code that extends the Monte Carlo simulation from our class example as follows. Suppose we have a game like the craps example in class, in which a single turn is defined by a player rolling dice until a certain sum is rolled. This game has a few extra wrinkles, however: the dice may have 6 or fewer sides, and the sides may be weighted any way the user likes. Further, the user may specify the number of dice rolled and the sum needed to end a turn.
Your program should run through a number of simulated turns to approximate the average number of rolls it takes to reach the specified sum. The number of turns in your simulation are decided by the user.
Details:
On the first line of input, the user inputs six nonnegative integers, separated by a single space, which add up to 100. These represent the percent chance of rolling a 1, 2, 3, 4, 5, or 6 on a weighted die. A \"0\" entry corresponds to that side not existing. For example, if the user enters
23 35 1 20 0 21
This corresponds to a weighted 5-sided die with a 23% chance of rolling 1, a 35% chance of rolling 2, a 1% chance of rolling 3, a 20% chance of rolling 4, and a 21% chance of rolling 6. (There is a 0% chance of rolling a 5 -- this corresponds to side 5 not existing).
You may assume the user always enters 6 nonnegative integers that sum to 100, separated by a single space. (No space is entered by the user before the first integer or after the last.)
On the second line of input, the user enters an integer giving the number of identical dice of this type to roll.
On the third line of input, the user enters an integer denoting the sum that causes a turn to end. You may assume the user always enters a value that is possible to reach as a sum of rolling the dice entered.
On the fourth line of input, the user enters an integer for the number of turns he or she would like to be simulated.
The output of the program should be the average number of rolls it took to reach the sum (per turn). The output should be formatted to contain 2 decimal places.
Using functions: Your code should use at least two separate functions to conduct the operations of: rolling a single die, and playing a single turn of the game. Use appropriate parameters and return values.
Examples of console input and output are below. User input is in red.
Example 1:
Enter the weights of each side of a die: 15 15 15 15 15 25
Enter the number of dice to roll: 2
Enter the sum to end a turn: 7
Enter the number of turns to simulate: 100000
The average number of rolls it took to finish a turn is: 6.06
Example 2:
Enter the weights of each side of a die: 22 0 78 0 0 0
Enter the number of dice to roll: 3
Enter the sum to end a turn: 9
Enter the number of turns to simulate: 100000
The average number of rolls it took to finish a turn is: 2.11
Note: Be sure to seed your random number generator appropriately so every execution of your program results in different random numbers being generated. Due to this randomness, in general, your program should not output the exact same answer every time, given the same inputs. You should see some (small) variation in output if you execute your program more than once with the same inputs. The above examples give a sample output from one program execution. You may see a slight variation in the output if you enter the same data in your own program.
Solution
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int faceOfDice( int percentChance[6], int chn ){
//chn is number from 0 to 100
//if percentChance is [10,11,21,31,10,17], then if 0<=chn<10, side 1 is chosen
//if 10 <= chn < 10+11 , side 2 is chosen, if 10+11 <= chn < 10+11+21, then side 3 is chosen and so on
for(int i =0; i < 6; i++ ){
if( chn < percentChance[i] ){ return i+1; }
chn-= percentChance[i];
}
}
int main(){
srand(time(NULL));
cout << \"Enter the weights of each side of a die: \";
int percentChance[6];
for(int i =0; i < 6; i++){
cin >> percentChance[i];
if( i != 0 ){ percentChance[i] += percentChance[i-1]; }
}
int nDice, sumToEnd, nTurns;
cout << \"Enter the number of dice to roll: \";
cin >> nDice;
cout << \"Enter the sum to end a turn: \";
cin >> sumToEnd;
cout << \"Enter the number of turns to simulate: \";
cin >> nTurns;
double averageRolls = 0.0;
for(int i =0; i < nTurns; i++){
int rolls = 0;
while(1){ //until we get the required sum, roll all the dices
rolls++;
int sum = 0;
for(int dice=0; dice < nDice; dice++ ){
int chn = rand()%100 + 1; //for each dice, number in range 1 to 100, to see which face of dice this // roll gave
sum += faceOfDice( percentChance, chn );
}
if( sum == sumToEnd ){ //if found the sum ,come out of the while loop
break;
}
}
averageRolls+= rolls;
}
cout << \"The average number of rolls it took to finish a turn is: \" << averageRolls/nTurns << endl;
return 0;
}


