I need help writing C++ code for this: Program Specifications: (2) Get from the
ID: 3666720 • Letter: I
Question
I need help writing C++ code for this:
Program Specifications:
(2) Get from the user the number of times they want to roll a pair of dice.
(3) Generate pseudorandom numbers to represent a roll of 2 dice. Be sure to call the rand function for each die value. Each die will have 6 sides, numbered 1 to 6. A single score is the sum of these 2 dice.
(4) Now make a loop that will do this the amount of times the user asked for. What type of loop is most appropriate?
(5) Count how many times a score of 2 is rolled, a score of 3 is rolled, a score of 4 is rolled, and so on up to a roll of 12. Output these counts. Example table output:
# of times each score was rolled
2: 33
3: 64
4: 81
5: 99
6: 162
7: 163
8: 124
9: 106
10: 84
11: 50
12: 34
(6) Now calculate and output the probability of rolling each score (the percentage of times each score was rolled). Be sure you are using floating-point division and not integer division here. Example output: (Do not format output.)
Probability of rolling each possible score
2: 3.3%
3: 6.4%
4: 8.1%
5: 9.9%
6: 16.2%
7: 16.3%
8: 12.4%
9: 10.6%
10: 8.4%
11: 5%
12: 3.4%
Explanation / Answer
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int n,i;
int dice1,dice2;
srand(time(NULL));
cout<<"Enter no. of time you want to roll the dice::";
cin>>n;
int score[15];
for(i=0;i<15;i++)
score[i] = 0;
for(i=0;i<n;i++)
{
dice1 = rand()%6+1;
dice2 = rand()%6+1;
score[dice1+dice2]++;
}
cout<<"# of times each score was rolled ";
int sum = 0;
for(i=2;i<=12;i++)
{
cout<<i<<": "<<score[i]<<endl;
sum+=score[i];
}
cout<<"Probability of rolling each possible score ";
for(i=2;i<=12;i++)
cout<<i<<": "<<score[i]*100.0/sum<<"%"<<endl;
return 0;
}