Coin Toss Help

closed account (2NyT0pDG)
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
#include <iostream>
#include "dice.h"
using namespace std;

int main() {

	
	Dice	coin(2);
	int		loop;
	int		counter[2];
	int		coinValue;

	for (loop = 1; loop < 2; loop++) {
		counter[loop] = 0;
	}

	// 10,000 times
	for (loop = 0; loop < 10000; loop++) {
		coinValue = coin.Roll();
		if (coinValue == 1) counter[coinValue]++;
		else if (coinValue == 2) counter[coinValue]++;
	}

	cout << "Coin Toss Count\n----------------------\n\n";
	for (loop = 1; loop < 2; loop++) {
		cout << "Flipped a " << loop << ", " << counter[loop] << " times in 10,000 flips.\n";
	}

	cout << endl;

	return 0;
}


I'm trying to use a class for a dice program as a coin toss simulator. For some reason, every time I try to run the program it says that there is a corruption of a stack of some kind surrounding "counter". What might be causing that?
Also, how would I go about making it so that the program read the longest series of heads or tails in a simulation of 10,000 flips?
Any help would be appreciated. Thanks.
i can't help without seeing the dice class, i don't see anything problematic in your main function

and i don't quiet understand what you are trying to achieve :
Also, how would I go about making it so that the program read the longest series of heads or tails in a simulation of 10,000 flips?


do you mean like the consecutive heads or tails in a row?
Last edited on
My guess is that somewhere coin.Roll() is returning a number bigger than 1 which is outside the bounds of counter.
@Trilaque

The first thing that I see is wrong, is you are trying to increase the value in array segment 2, which actually doesn't exist. When you created counter[2], you have a counter[0] and counter[1]. Arrays start with zeroes. The best thing I can think of to remedy that, is make coinValue a bool, which is either a 0 or a 1. When you flip the coin, a 0 could be heads, and 1, tails. . Hope this helps..

The first thing that I see is wrong, is you are trying to increase the value in array segment 2, which actually doesn't exist. When you created counter[2], you have a counter[0] and counter[1]. Arrays start with zeroes. The best thing I can think of to remedy that, is make coinValue a bool, which is either a 0 or a 1. When you flip the coin, a 0 could be heads, and 1, tails. . Hope this helps..


good call, that is true :) I missed that
as for your consecutive flips problem

try making 4 variables,

int currentcnt1, currentcnt2, stored1, stored2, previous;

if previous == current coin flip
increment the current coin flipped currentcnt

when it changes, store the currentcnt into stored

reset currentcnt

and repeat...

seems like the best way in my head
@oonej
try making 4 variables,


Typo? You show 5 variables ;)
yeah, 5 .. oyu can just use that idea... and do it how ever you like :)
Topic archived. No new replies allowed.