Dice Program with number of results

I'm tasked to simulate the roll of two dice, generate two random integers between 1 & 6 inclusive, add them together, and use that minus two to index an array to accumulate the count of how many time each result occurred for 10,000 rolls of the two dice.

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
  #include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <ctime>
#include <cmath>
using namespace std;
int roll();
ostream& printArrays(ostream&, int[], int);
ostream& printHeadings(ostream&);
//**********************************************************************
int main()
{
	int dice[12] = { 0 };
	int value = 0;
	int rolls;
	rolls = roll();
	printArrays(cout, dice, value);	
	return 0;
}
//*********************************************************************
//Calculates the rolling of two dice
int roll()
{
	int die1,
		die2,
		sum = 0,
		value = 0,
		count[12] = {0};
		srand(time(NULL));
	for (int i = 1; i< 10000; i++)
	{
		die1 = rand() % 6 + 1;
		die2 = rand() % 6 + 1;
		sum = die1 + die2;
		value++;
		value = count[sum - 1]++;
	}
	return value;
}
//**********************************************************************
//Prints Actual, Expected, and Percent Error Values
ostream& printArrays(ostream& outFile, int dice[], int value)
{
	const double odds[11] = { 1 / 36., 2 / 36., 3 / 36., 4 / 36., 5 / 36., 
	6 / 36., 5 / 36., 4 / 36., 3 / 36., 2 / 36., 1 / 36. };
	double	predicted,
			pcterror;
	int		predictedint;
	printHeadings(outFile) << endl;
	for (int i = 0; i < 12; i++)
	{
		predicted = odds[i] * 0.01 * value;
		pcterror = (dice[i] - predicted / predicted * 100);
		predictedint = static_cast <int> (predicted);
		outFile << setw(6) << value << setw(9) << dice[i]
				<< setw(9) << predictedint
				<< setw(9) << setprecision(3) << fixed << pcterror << endl;
	}
	return outFile;	
}
//*************************************************************************
//Prints Value Headings
ostream& printHeadings(ostream& outFile)
{
	outFile << setw(6) << "Value" << setw(9) << "Actual" << setw(9)
		<< "Expected" << setw(9) << "Percent" << endl;
	outFile << setw(6) << "" << setw(9) << "Count" << setw(9)
		<< "Count" << setw(9) << "Error" << endl;
	return outFile;
}


The output only returns a bunch of 0's. I know I did something dumb but I'm not sure where any help would be appreciated.
closed account (48bpfSEw)
0 output is often a phenomenon of data conversion from float to int. values in float less than 0 eg. 0,1234 will be convertet to 0 as integer.

I am using this code to debug values:

{wostringstream os; os << ""; OutputDebugString(os.str().c_str());}

in combination with DebugView (www.sysinternals.com) which receives the Strings from OutputDebugString.
Last edited on
The problem is that in main() you pass the array dice with only zeros to printArrays and also value is 0. Should roll() receive an array to fill? What is the meaning of value it returns?
roll is supposed to roll two dice randomly and give a value.

int main or roll whichever would be better should roll 10000 different sets and give how many of each value (2-12) come up, how many were expected to come up, and percentage of error.
I have switched up some of the code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <ctime>
#include <cmath>
using namespace std;
int roll();
ostream& printArrays(ostream&, int[], int);
ostream& printHeadings(ostream&);
//**********************************************************************
//**********************************************************************
int main()
{
int dice[12] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int rolls;
srand(time(0));
for (int value = 1; value < 10000; value++)
{
rolls = roll();
value++;
value = dice[rolls - 2]++;
}
printArrays(cout, dice, rolls);
return 0;
}
//*********************************************************************
//Calculates the rolling of two dice
int roll()
{
int die1,
die2,
sum = 0;
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
return sum;
}
//**********************************************************************
//Prints Actual, Expected, and Percent Error Values
ostream& printArrays(ostream& outFile, int dice[], int rolls)
{
const double odds[11] = { 1 / 36., 2 / 36., 3 / 36., 4 / 36., 5 / 36.,
6 / 36., 5 / 36., 4 / 36., 3 / 36., 2 / 36., 1 / 36. };
double predicted,
pcterror;
int predictedint;
printHeadings(outFile) << endl;
for (int i = 0; i < 12; i++)
{
predicted = odds[i] * 0.01 * rolls;
pcterror = (dice[i] - predicted / predicted * 100);
predictedint = static_cast <int> (predicted);
outFile << setw(6) << rolls << setw(9) << dice[i]
<< setw(9) << predictedint
<< setw(9) << setprecision(1) << fixed << pcterror << endl;
}
return outFile;
}
//*************************************************************************
//Prints Value Headings
ostream& printHeadings(ostream& outFile)
{
outFile << setw(6) << "Value" << setw(9) << "Actual" << setw(9)
<< "Expected" << setw(9) << "Percent" << endl;
outFile << setw(6) << "" << setw(9) << "Count" << setw(9)
<< "Count" << setw(9) << "Error" << endl;
return outFile;
}

It is output to value, Acutal and percent but it is outputting the wrong values. the Value column should be dice options (2-12) Actual Count should how many times each got rolled within 10,000 rolls. Expected and percent should be based on the odds. Please Help!
One problem is initializing and filling the array in main()
int dice[12] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
All values should be 0 and you also need only 11 elements.
Try your loop like this:
1
2
3
4
5
for (int value = 0; value < 10000; value++)
  {
    rolls = roll();
    dice[rolls - 2]++;
  }


Why do you pass the result from the last throw to printArrays() ? printArrays(cout, dice, rolls);
Topic archived. No new replies allowed.