this is c++ programming. please, try sample run1, sample run 2. thank you ! On t
ID: 3838867 • Letter: T
Question
this is c++ programming.
please, try sample run1, sample run 2.
thank you !
On the first line of input, the user inputs six nonnegative integers, separated by a single space, which addup 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 weighted5-sided die with a 23% chance of rolling 1,a 35% chance of rolling2, a 1% chance of rolling 3, a 20% chance of rolling 4, and a 21% chance of rolling 6. (There is a O% chance of rolling a 5--this corresponds to side 5 not existing.) You may assume the user always enters6 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 typeto 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 bethe average number of rolls ittook to reach the sum (per turn). The output should be formatted tocontain 2 decimal places. Using functions: Your code should use at least two separate functions to conduct the operations of rolling asingle die, and playing a single turn ofthe game. Use appropriate parameters and return values. Note: your functions may end up taking in a lot of parameters! This is OK.Explanation / Answer
#include <random>
#include <vector>
#include <array>
#include <iostream>
#include <memory>
/* Dice Model*/
struct dice{
private:
std::unique_ptr<std::random_device> rd;
std::mt19937 gen;
std::discrete_distribution<> distr;
public:
dice(dice&& left) : rd(std::move(left.rd)), gen((*rd)()), distr(left.distr){
}
dice(const std::array<int, 6>& probability) :
rd(std::make_unique<std::random_device>()),
gen((*rd)()),
distr(probability.begin(), probability.end()) {
}
int roll(){
return distr(gen) + 1;
}
};
/*Model of the Experiment*/
struct diceModel{
private:
std::vector<dice> dices;
int target;
public:
diceModel(std::array<int, 6> probability, int numDice, int target) :
target(target)
{
for(int i = 0; i < numDice; ++i){
dices.push_back(dice(probability));
}
}
bool turn()
{
int sum = 0;
for(auto& diceE : dices){
sum += diceE.roll();
}
return sum == target;
}
};
/* Get Average Number of Rolls for a Target Turn*/
double getAverageNumberOfRolls(std::array<int, 6> probability, int numDice, int target,
int turns)
{
diceModel model(probability, numDice, target);
int R = 0;
int N = 0;
int S = 0;
for(int i = 0; i < turns; ++i){
++N;
if(model.turn()){
S += N;
++R;
N = 0;
}
}
return 1.0*S/R;
}
int main(){
std::array<int, 6> probability;
std::cout << " Enter the weight of each side of Die :";
for(int i = 0; i < 6; ++i) std::cin >> probability[i];
int numDice;
std::cout << " Enter the number of Dice to Roll :";
std::cin >> numDice;
int target;
std::cout << " Enter the sum to end a Turn : ";
std::cin >> target;
int turns;
std::cout << " Enter the number of Turns to simulate : ";
std::cin >> turns;
std::cout << " The average number of rolls it took to finish a turn is : ";
std::cout << getAverageNumberOfRolls(probability, numDice, target, turns) << std::endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.