Hello. I am in need of assistance; currently working on a program that ask the user to input a certain amount of dice rolls to be simulated (in my case 500) using 4-dice. The program then keeps track of how many times a certain number between 4 and 24, the minimum amount all four dice could sum up to and the maximum, and print a bar graph histogram using "X"s. I have managed to get the program to compile and run but when it comes to printing the correct number of rolls in the form of "X"s, it goes on. Here is one example of what the program could look like:
How many rolls do you want? 500 (this is the user input)
4:
5:X
6:XXX
7:XXXXXXXXXXXXXX
8:XXXXXXXXXXXXXX
9:XXXXXXXXXXXXXXXXXXXXXXX
10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
11:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
12:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
13:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
16:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
17:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
18:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
19:XXXXXXXXXXXXXXXXXX
20:XXXXXXXXXX
21:XXX
22:XX
23:
24:X
In this example, we asked the program to simulate 500 4-dice rolls. The sum was
never four, the sum was five on 1 of the 500 trials, the sum was six on 3 of the trials,
etc.
Help would be greatly appreciated! Thank you in advance. Below is my current code. The idea behind it is, it calls function roll(), takes that result and stores it in variables roll(1-4), using a random roll function. Afterwards it adds each up and gets a sum. This rollsum would then go into an if-else-if, that checks to see which of the possible 4-24 totals it landed on. Once the correct number is found, a counter is incremented and stored in an array. Once the total rolls are simulated and all counters are incremented correctly in their correct array index, the array is passed on to function printHistogram(). Here is where the printing of a similar image found above is suppose to take place.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
//MAY NEED TO SEED THE PROGRAM IF THE NUMBERS DO NOT CHANGE..
int roll()
{
int d1 = 0;
srand(time(0));
for(int i=1;i<21; i++)
{
d1 = rand() % 6+1;
}
cout << endl;
return d1;
}
void printHistogram(int list[21], int z)
{
for(int j = 0; j < 21; j++)
{
int k = 0;
int counts[21];
counts[j] = list[j];
k = counts[j];
if(counts[j] == 0)
{
cout << j+4 << ": ";
}
else
{
cout << j+4 << ": " << endl;
for(int p = 0; p < k; p++)
cout << "X";
}
}
}
int main()
{
//OUTPUT TO USER TO INPUT NUMBER OF DICE ROLLS
cout << "Please enter how many roll simluations: " << endl;
int input;
cin >> input; //INPUT FROM USER FOR DICE ROLLS
int list[21]; //CREATES ARRAY OF 21 SLOTS
for(int i=0; i<21; i++)
list[i]; //CATEGORIZES ARRAY
int count4, count5, count6, count7, count8, count9, count10, count11, count12, count13, count14, count15, count16, count17, count18, count19, count20, count21, count22, count23, count24 = 0;
for(int x = 0; x<= input; x++) //FOR ARRAY COUNTER
{
int y = 0;
int roll1 = roll();
int roll2 = roll();
int roll3 = roll();
int roll4 = roll();
int rollsum = roll1 + roll2 + roll3 + roll4;
//cout << rollsum; this was merely an output to ensure function roll() was being called correctly
if(rollsum == 4)
{
count4++;
y = 0;
list[y] = list[y]+count4;
}
else if(rollsum == 5)
{
count5++;
y = 1;
list[y] = list[y]+count5;
}
else if(rollsum==6)
{
count6++;
y = 2;
list[y] = list[y]+count6;
}
else if(rollsum == 7)
{
count7++;
y = 3;
list[y] = list[y]+count7;
}
else if(rollsum==8)
{
count8++;
y = 4;
list[y] = list[y]+count8;
}
else if(rollsum==9)
{
count9++;
y = 5;
list[y] = list[y]+count9;
}
else if(rollsum == 10)
{
count10++;
y = 6;
list[y]=list[y]+count9;
}
else if(rollsum == 11)
{
count11++;
y = 7;
list[y] = list[y]+count10;
}
else if(rollsum==12)
{
count12++;
y = 8;
list[y] = list[y]+count12;
}
else if(rollsum == 13)
{
count13++;
y = 9;
list[y] = list[y]+count13;
}
else if(rollsum==14)
{
count14++;
y = 10;
list[y] = list[y]+count14;
}
else if(rollsum==15)
{
count15++;
y = 11;
list[y] = list[y]+count15;
}
if(rollsum == 16)
{
count16++;
y = 12;
list[y] = list[y]+count16;
}
else if(rollsum == 17)
{
count17++;
y = 13;
list[y] = list[y]+count17;
}
else if(rollsum==18)
{
count18++;
y = 14;
list[y] = list[y]+count18;
}
else if(rollsum == 19)
{
count19++;
y = 15;
list[y] = list[y]+count19;
}
else if(rollsum==20)
{
count20++;
y = 16;
list[y] = list[y]+count20;
}
else if(rollsum==21)
{
count21++;
y = 17;
list[y] = list[y]+count21;
}
else if(rollsum == 22)
{
count22++;
y = 18;
list[y] = list[y]+count22;
}
else if(rollsum == 23)
{
count23++;
y = 19;
list[y] = list[y]+count23;
}
else
{
count24++;
y = 20;
list[y] = list[y]+count24;
}
}
printHistogram(list, 21);
return 0;
}
|