Question
I need to simulate the roll of 2 six-sided dice using rand() repeatedly, according to how many trials the user wanted to run. Then, report
I need to simulate the roll of 2 six-sided dice using rand() repeatedly, according to how many trials the user wanted to run. Then, report the percentage of how many of the trials yielded a given sum for each of the possible sums from 2 to 12. (In C++) //The following is the code I've worked out so far. The main focus of this program is arrays, loops, and random numbers. As you can //tell right away, there are no arrays so could I get help in inserting my progam in an array to run more efficiently as well as look over //any other thing I might have missed and if I got the percentages to display correctly. Thank you! Will rate (:
/*********************************************************************************************************************************************************/
#include
#include
using namespace std;
int main() {
double two=0, three=0, four=0, five=0, six=0, seven=0,
eight=0, nine=0, ten=0, eleven=0, twelve=0;
int dice1;
int dice2;
double total;
srand(time(NULL));
cout<<"How many times do you want to throw a pair of six-sided dice?"< int numTrials; cin>>numTrials; for(int i=0; i { dice1 = rand()%6 + 1; dice2 = rand()%6 + 1; total = dice1 + dice2; if (total == 2) { two++; } else if(total == 3) { three++; } else if(total == 4) { four++; } else if(total == 5) { five++; } else if(total == 6) { six++; } else if(total == 7) { seven++; } else if(total == 8) { eight++; } else if(total == 9) { nine++; } else if(total == 10) { ten++; } else if(total == 11) { eleven++; } else { twelve++; } } two = two / numTrials * 100.0; three = three / numTrials * 100.0; four = four / numTrials * 100.0; five = five / numTrials * 100.0; six = six / numTrials * 100.0; seven = seven / numTrials * 100.0; eight = eight / numTrials * 100.0; nine = nine / numTrials * 100.0; ten = ten / numTrials * 100.0; eleven = eleven / numTrials * 100.0; twelve = twelve / numTrials * 100.0; cout<<"The distribution of results from the "< cout<<"2: "< cout<<"3: "< cout<<"4: "< cout<<"5: "< cout<<"6: "< cout<<"7: "< cout<<"8: "< cout<<"9: "< cout<<"10: "< cout<<"11: "< cout<<"12: "< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started