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

Write a program that simulates the rolling of two dice. Using rand() function ro

ID: 3631727 • Letter: W

Question

Write a program that simulates the rolling of two dice. Using rand() function roll the two dice one after the other. Use srand(time(0)); for setting the initial seed value. The sum of the two dice values in any through will be between 2 and 12. The probabilities for the various sums are: for 2 and for 12; 2.778%; for 3 and for 11: 5.556%; for 4 and for 10: 8.333%; for 5 and 9: 11.111%; for 6 and 8: 13.889; and for 7: 16.667%. Roll the two dice in your program 10,000 times, add the dice values in each throw and increment the appropriate sum value for the result. When all the 10,000 throws are completed, compute the probability for each of the sums (2 to 12). The program should output the results under three columns: the first one shows the sum, the second shows the theoretical probability and the third shows the observed probability.

Explanation / Answer

please rate - thanks

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include <ctime>
using namespace std;
int main()
{
int dice,i,j,k,max=10000,count[11]={0};
double sum,expected[11],actual,percent;
srand(time(0));
//calculate actual
for(k=2;k<=12;k++)
{expected[k-2]=0;
for(i=1;i<=6;i++)
     for(j=1;j<=6;j++)
        {if(i+j==k)                  //count how many times each number occurs
            expected[k-2]++;
         }
}
sum=0;
for(i=0;i<11;i++)
    sum+=expected[i];                //get how many total combinations there are
percent=1./sum;                       //change to a percent
for(i=2;i<13;i++)
    {//cout<<expected[i-2]<<endl;
    expected[i-2]=percent*expected[i-2]*100.;
     //cout<<i<<""<<sum<<" "<<expected[i-2]<<endl;
    }        
for(i=0;i<max;i++)
    {dice=(rand()%6+1)+(rand()%6+1);
     count[dice-2]++;
    }
cout<<"Sum Expected Actual ";
for(i=2;i<13;i++)
{actual=(count[i-2]/(double)max)*100.;
cout<<setw(3)<<i<<setw(10)<<setprecision(3)<<fixed;
cout<<expected[i-2]<<"%"<<setw(15)<<actual<<"% ";
  
}
system("pause");
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