Program Specifications: (2) Get from the user the number of times they want to r
ID: 3696968 • Letter: P
Question
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:
(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.)
This is what we are given to start:
#include
//Include library necessary for rand function
using namespace std;
int main() {
srand(1111); // Do not change the seed value.
// Declare all necessary variables
cout << "Enter number of times you want to roll a pair of dice: ";
// Get user input
cout << endl;
// Calculate roll counts
cout << "# of times each score was rolled" << endl;
// Output roll count table
cout << endl;
cout << "Probability of rolling each possible score" << endl;
// Output percentage table
return 0;
}
Explanation / Answer
# include <stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main() {
Srand(1111);
int c1=0,c2=0,c3=0,c4=0,c5=0,c6=0;
int f1=0,f2=0,f3=0,f4=0,f5=0,f6=0;
int n; int sum =0; int count =0;
int face; int face 1;
cout<< “Enter no of times you want to roll a pair of dice:”;
cin>>n;
cout<,end1;
for(int i=1;i<=n;i++) {
face = 1 + rand() %6;
face1 = 1 + rand() %6;
switch(face) {
case 1: ++c1; break;
case 2: ++c2; break;
case 3: ++c3; break;
case 4: ++c4; break;
case 5: ++c5; break;
case 6: ++c6; break; }
switch(face1) {
case 1: ++f1; break;
case 2: ++f2; break;
case 3: ++f3; break;
case 4: ++f4; break;
case 5: ++f5; break;
case 6: ++f6; break; }
switch(face) {
case 1: sum = c1+f1; break;
case 2: sum = c2+f2;break;
case3 : sum = c3 + f3;break;
case4: sum = c4+f4; break;
case5:sum =c5+f5; break;
case 6: sum = c6+f6; break; }
if(sum == 2) count ++;
else if(sum == 3) count++;
else if(sum == 4) count++;
else if(sum == 5) count++;
else if(sum == 6) count++;
else if(sum == 7) count++;
else if(sum == 8) count++;
else if(sum == 9) count++;
else if(sum == 10) count++;
else if(sum == 11) count++;
else if(sum == 12) count++;
}
cout << " No of times each score was rolled" << endl;
for(int i=2;i<=12;i++ {
cout << i <<”%13d ” << count; }
cout << endl;
cout << "Probability of rolling each possible score" << endl;
for(int i=2;i<=12;i++ {
float p =10 % * count;
cout<< i << “%f” << p; }
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.