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

I need to simulate the roll of 2 six-sided dice using rand() repeatedly, accordi

ID: 3879550 • Letter: I

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

#include <stdlib.h>

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?"<<endl;

int numTrials;

cin>>numTrials;

  

for(int i=0; i<numTrials; 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 "<<numTrials<<" trials is:"<<endl;

cout<<"2: "<<two<<endl;

cout<<"3: "<<three<<endl;

cout<<"4: "<<four<<endl;

cout<<"5: "<<five<<endl;

cout<<"6: "<<six<<endl;

cout<<"7: "<<seven<<endl;

cout<<"8: "<<eight<<endl;

cout<<"9: "<<nine<<endl;

cout<<"10: "<<ten<<endl;

cout<<"11: "<<eleven<<endl;

cout<<"12: "<<twelve<<endl;

  

return 0;

}

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include<time.h>
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;*/
  
  
//declaring array
double per[13];
for(int k=0;k<13;k++)per[k]=0;
  
int dice1;
int dice2;
int total;
srand(time(NULL));
  
cout<<"How many times do you want to throw a pair of six-sided dice?"<<endl;
int numTrials;
cin>>numTrials;
  
for(int i=0; i<numTrials; i++)
{
dice1 = rand()%6 + 1;
dice2 = rand()%6 + 1;
total = dice1 + dice2;
per[total]=per[total]+1;
/*
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++;
}*/
}
  
for(int k=0;k<13;k++)
per[k] = (per[k]*1.0 / numTrials )* 100.0;
/*
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 "<<numTrials<<" trials is:"<<endl;
for(int k=0;k<13;k++)
if(k>1)
cout<<k<<": "<<per[k]<<endl;
   /*
   cout<<"2: "<<two<<endl;
cout<<"3: "<<three<<endl;
cout<<"4: "<<four<<endl;
cout<<"5: "<<five<<endl;
cout<<"6: "<<six<<endl;
cout<<"7: "<<seven<<endl;
cout<<"8: "<<eight<<endl;
cout<<"9: "<<nine<<endl;
cout<<"10: "<<ten<<endl;
cout<<"11: "<<eleven<<endl;
cout<<"12: "<<twelve<<endl;
*/
return 0;
}

output:

How many times do you want to throw a pair of six-sided dice?
10
The distribution of results from the 10 trials is:
2: 0
3: 10
4: 0
5: 20
6: 20
7: 30
8: 20
9: 0
10: 0
11: 0
12: 0


Process exited normally.
Press any key to continue . . .