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

Write an application to simulate the rolling of two dice. The application should

ID: 3848596 • Letter: W

Question

Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show as integer value from 1 to 6, so the sum of the value will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Figure below shows the 36 possible combinations of the two dice. Your application should roll the dice 36,000 times Use a one dimensional array to keep track of the number of times each possible sum appears. Display the results in tabular format. Determine whether the totals are reasonable (e.g., there are six ways to roll a 7, so approximately one-sixth of the rolls should be 7).

Explanation / Answer

C++ code:

#include <iostream>   
#include <stdlib.h>   
#include <time.h>   
using namespace std;
int main ()
{
int array[11];
for (int i = 0; i < 11; ++i)
   {
       array[i] = 0;  
   }  
srand (time(NULL));
for (int i = 0; i < 36000; ++i)
{
   int d1,d2;
   d1 = rand() % 6 + 1;
   d2 = rand() % 6 + 1;
   int sum = d1 + d2;

   if(sum == 2)
   {array[0] = array[0] + 1; }
   else if(sum == 3)
   {array[1] = array[1] + 1; }
   else if(sum == 4)
   {array[2] = array[2] + 1; }
   else if(sum == 5)
   {array[3] = array[3] + 1;   }
   else if(sum == 6)
   {array[4] = array[4] + 1;   }
   else if(sum == 7)
   {array[5] = array[5] + 1;   }
   else if(sum == 8)
   {array[6] = array[6] + 1;   }
   else if(sum == 9)
   {array[7] = array[7] + 1;   }
   else if(sum == 10)
   {array[8] = array[8] + 1;   }
   else if(sum == 11)
   {array[9] = array[9] + 1;   }
   else if(sum == 12)
   {array[10] = array[10] + 1; }
}
cout << "Sum" << " " << "Frequency" << " " << "Percentage" << endl;
for (int i = 0; i < 11; ++i)
{
       cout << i+2 << " " << array[i] << " " << (array[i]/36000.0)*100.0 << endl;
}

return 0;
}

Sample Output:

Sum Frequency Percentage
2 1026 2.85
3 2000 5.55556
4 2977 8.26944
5 3889 10.8028
6 5026 13.9611
7 5978 16.6056
8 5014 13.9278
9 4156 11.5444
10 2956 8.21111
11 1982 5.50556
12 996 2.76667

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