My program needs to give the user the option of how many six-sided dice to throw
ID: 3880749 • Letter: M
Question
My program needs to give the user the option of how many six-sided dice to throw followed by the number of dice being thrown. Then output the percentages of each total from the dice that occured that number of throws. The user enters the number of dice they want to roll, say 4 dice for example. Then, for each roll, you simulate rolling 4 dice and get the total. After all of the trials you display the percentage of each possible sum. In the case of 4 dice, you would print the percentages for each possible sum from 4 (the lowest possilbe sum for 4 dice) to 4*6=24 (the largest possilbe sum). Please help, the following is my code but it doesn't come out with the correct output for the percentages. Also I think I need to implement an array of pointers and then a class of a stack, but i'm not sure how. Thanks in advance, Will rate! (:
/**************************************************************************************************************************************/
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
#include<sstream>
using namespace std;
int main(){
int numdice, numthrows;
srand(time(NULL));
cout << "Insert number of dice: ";
cin >> numdice;
cout << "Insert number of throws: ";
cin >> numthrows;
float *items = new float[numdice*6 + 1];
for (int i = 0; i<numthrows; i++){
int sum = 0;
for (int j = 0; j<numdice; j++){
sum = sum + rand()% 6 +1;
}
items[sum]++;
}
for (int i = numdice; i<=6 * numdice; i++){
stringstream ss;
stringstream ss1;
ss<<i<<":";
double percentage = items[i]/numthrows;
ss1 << setprecision(2) << percentage;
string str1 = ss1.str();
str1 = str1.substr(1,3);
cout << setw(15) << left << ss.str() << str1 << endl;
}
}
Explanation / Answer
Hello, I have a solution for you. Your problem was because of the random garbage value filled in the array , which was giving invalid outputs. I have solved it by initializing the array completely with zeroes filled in all the indices. Now the code works perfectly, and I am attaching the output along with this.
The modified code is highlighted in bold text.
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
#include<sstream>
using namespace std;
int main(){
int numdice, numthrows;
srand(time(NULL));
cout << "Insert number of dice: ";
cin >> numdice;
cout << "Insert number of throws: ";
cin >> numthrows;
int n=numdice*6 + 1;
/**
initializing an array with n capacity, and filling it with
zeroes. This is to ensure that no garbage value is taken*/
float items[n];
for(int i=0;i<n;i++){
items[i]=0;
}
for (int i = 0; i<numthrows; i++){
int sum = 0;
for (int j = 0; j<numdice; j++){
sum = sum + rand()% 6 +1;
}
items[sum]++;
}
for (int i = numdice; i<=6 * numdice; i++){
stringstream ss;
stringstream ss1;
ss<<i<<":";
double percentage = items[i]/numthrows;
ss1 << setprecision(2) << percentage;
string str1 = ss1.str();
str1 = str1.substr(1,3);
cout << setw(15) << left << ss.str() << ss1.str()<<" %" << endl;
}
}
/*OUTPUT*/
Insert number of dice: 6
Insert number of throws: 4
6: 0 %
7: 0 %
8: 0 %
9: 0 %
10: 0 %
11: 0 %
12: 0 %
13: 0 %
14: 0 %
15: 0.5 %
16: 0 %
17: 0 %
18: 0 %
19: 0 %
20: 0 %
21: 0.25 %
22: 0 %
23: 0 %
24: 0 %
25: 0.25 %
26: 0 %
27: 0 %
28: 0 %
29: 0 %
30: 0 %
31: 0 %
32: 0 %
33: 0 %
34: 0 %
35: 0 %
36: 0 %
Insert number of dice: 2
Insert number of throws: 5
2: 0 %
3: 0 %
4: 0.4 %
5: 0 %
6: 0.4 %
7: 0 %
8: 0.2 %
9: 0 %
10: 0 %
11: 0 %
12: 0 %
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.