1000 games of Craps stats (arrays)

Hello, I am working on this program that plays a simple game of craps but plays it 1000 times. Then I have it show me how times the is won on the first roll, second roll, and so forth. I also have done this for lost games as well. Then I have it also telling me the calculated odds at winning craps every 100 games!

But I need the program now to tell me the average length of the games played in rolls! For example "Average length of the game of craps = ?? rolls". Seems easy enough, but when I did avg = sum / size its giving a higher value than what I should be expecting. I am getting around 14 rolls as an average when a more than half of the games won or lost are in the first roll of the game!

I am basically deriving all my stats from arrays and counters. And I am not asking for someone to write the code for me, but I am seriously just stuck on the average of an array and I don't know what else to do! I would really appreciate any tips or advice, thank you in advance.

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
  #include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

int rollDice();
int oddWin(int, int);

int _tmain(int argc, _TCHAR* argv[])
{

	cout << setprecision(2) << fixed;

	enum Status{CONTINUE, WON, LOST};

	int myPoint, gamesPlayed, sumOfDice, wsum = 0, lsum = 0, a = 0, i, k, l, m;
	
	double avg, avgSum = 0.0;

	const int rollFreq = 35;
	
	static int wcount = 0, lcount = 0, roll = 0;
	
	int wround[rollFreq] = { 0 };
	int lround[rollFreq] = { 0 };

	Status gameStatus;
	
	srand(time(0));

	for (gamesPlayed = 1; gamesPlayed <= 1000; gamesPlayed++)
	{
		roll++;
		sumOfDice = rollDice();
		switch (sumOfDice)
		{
		case 7:
		case 11:
			gameStatus = WON;
			wcount++;
			break;
		case 2:
		case 3:
		case 12:
			gameStatus = LOST;
			lcount++;
			break;
		default:
			gameStatus = CONTINUE;
			myPoint = sumOfDice;
			break;
		}

		while (gameStatus == CONTINUE)
		{
			sumOfDice = rollDice();
			roll++;
			if (sumOfDice == myPoint)
			{
				gameStatus = WON;
				wcount++;
			}
			else
			if (sumOfDice == 7)
			{
				gameStatus = LOST;
				lcount++;
			}
		}

		if (gameStatus == WON)
			wround[roll]++;

		if (gameStatus == LOST)
			lround[roll]++;

		roll = 0;
	}

	//WON GAMES
	cout << "\nWins:" << endl;

	for (k = 1; k < rollFreq - 15; k++)
		cout << wround[k] << setw(13) << " games won on " << k << " rolls" << endl;

	for (i = 20; i < rollFreq -1; i++)
		wsum += wround[i];
	cout << wround[i] << setw(13) << " games won on 20 or more rolls" << endl;
	
	//LOST GAMES
	cout << "\n\nLoses: " << endl;

	for (m = 1; m < rollFreq-15; m++)
		cout << lround[m] << setw(13) << " games lost on " << m << " rolls" << endl;

	for (l = 20; l < rollFreq -1; l++)
		lsum += lround[l];
	cout << lround[l] << setw(13) << " games lost on 20 or more rolls\n\n" << endl;

	//ODDS OF WINNING
	oddWin(wcount, gamesPlayed);

	//AVGWIN
        //IM STUCK HERE

	system("pause");
	return 0;
}

int rollDice()
{
	int die1 = 1 + rand() % 6;
	int die2 = 1 + rand() % 6;
	int sum = die1 + die2;

	return sum;
}

int oddWin(int x, int y)
{
	int c = 1000;
	double odds;

	for (; c > 0; c= c - 100)
	{
		odds = (x / (static_cast<double>(y / c)));
		odds = (odds / c) * 100;

		cout << "Odds of winning in " << c << " games = " << odds << "%" << endl;
	}

	return 0;
}


It's a little sloppy right now, but I am just trying to get the program to do what I need it to do.
> but when I did avg = sum / size
I don't see that in your code.

> tell me the average length of the games played in rolls
you have arrays `wround' and `lround' that counts the length of won and lost games. As you don't care why it terminates you need to sum them
t_j = w_j + l_j
Those are counts, to translate them to relative frequencies you divide over the total amount of games played.
p_j = t_j/sum(t_j)
Then compute the expected value
E = sum j*p_j


Other things
> wround[roll]++;
`roll' may excede the size of the array, you'll be accessing out of bounds then.

> odds = (x / (static_cast<double>(y / c)));
It will first perform the integer division, and later convert it to double. So you already truncated the result.
No idea what you are trying to do there, `x' never changes inside the loop.
Topic archived. No new replies allowed.