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

I need help writing C++ code for this: Program Specifications: (2) Get from the

ID: 3792877 • 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%

main.cpp 1 #include kios tream 2 Include library necessary for rand function using namespace std; 4 6 int main() 7 srand (3333); Do not change the seed value. 9 TODO: Declare all necessary variables 12 cout "Enter number of times you want to roll a pair of dice: 13 TODO: Get user input 14 cout endl 16 TODO: Calculate roll counts 18 19 cout endl 20 cout of times each score was rolled" endl 21 TODO: output roll count table Submit In "Develop" mode, you can run your program as oft Develop LL

Explanation / Answer

// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
srand(3333);

int iteration;
int sum;
int dice1, dice2;
int countArray[13] = {0};

cout << "Enter number of times you want to roll a pair of dice: ";
cin >> iteration;
cout << endl;

for (int i = 0; i < iteration; ++i)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;

sum = dice1 + dice2;

countArray[sum]++;
}

cout << endl;
cout << "# of times each score was rolled " << endl;
for (int i = 2; i < 13; ++i)
{
cout << i << ": " << countArray[i] << endl;

}

cout << endl;
cout << "Probability of rolling each possible score" << endl;
for (int i = 2; i < 13; ++i)
{
cout << i << ": " << (countArray[i]/(1.0*iteration))*100 << "%" << endl;
}

return 0;
}

/*
output:

Enter number of times you want to roll a pair of dice: 1000


# 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

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%

*/