switch case overflown array

Here's what the program does and where the problem is. It generates 100 random numbers between 2-12(simulating die throws with 2 dice at the same time), and then does a few things with the results, such as deciding whether there were more even or odd throws, or if there was a 12 as a result or not, the third part is supposed to sort the results into the result[12] array. After that it displays the array, but the numbers for the results are incorrect to say the least(they are around -800000000).
Where did i fuck up?
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
  #include <iostream>
#include <ctime>
using namespace std;

int main()
{
	srand((unsigned int)time(NULL));
	int i, throw[100],result[12],all=0,evens = 0;
	for (i = 0; i < 100; i++)
	{
		throw[i] = rand() % 11 + 2;	
		cout << throw[i] <<"\t";
		all = all + throw[i];
		if (throw[i] % 2 == 0)evens++;
		
	}

	cout << "\n Average throw: " << (float)all / 100<<"\n";
	i = 0;
	while (i < 100 && throw[i] != 12)
		i++;
	if (i < 100)cout << "there was a twelve.\n";
	else cout << "there were no twelves\n";
	if (evens > 50) cout << "There were more even throws\n";
	else if (evens < 50)cout << "There were more odd throws\n";
	else if (evens==50) cout <<"Odds and evens were even\n";
for(i=0;i<100;i++)    	{
		switch (throw[i])
		{
		case 2:result[0]++; break;
		case 3:result[1]++; break;
		case 4:result[2]++; break;
		case 5:result[3]++; break;
		case 6:result[4]++; break;
		case 7:result[5]++; break;
		case 8:result[6]++; break;
		case 9:result[7]++; break;
		case 10:result[8]++; break;
		case 11:result[9]++; break;
		case 12:result[10]++; 
		}
	}
	for (i = 0; i < 11; i++)
	{
		cout <<"There were"<<result[0]<<i+2<<"-s.";
	}


	system("pause");
	return 0;
}
Last edited on
throw is a keyword. I'm surprised your compiler lets you use it as a variable name.

What is dob?
My code has hungarian names for the integers and the arrays, dob is for throw, missed the one at the switch, i'll edit it in a second
Last edited on
The result array is uninitialized. You need to make sure all values has been set to zero before starting to increment. You can use std::fill (<algorithm>) ...
 
fill(begin(result), end(result), 0);
... or you can add an empty pair of curly brackets when defining the variable.
 
int i, throw[100], result[12]{}, all = 0, evens = 0;


Also note that you're only counting the 11 first values in the throw array.
Last edited on
I really don't understand why that is not a syntax error like it is with integers, but thanks for the help.
BTW i do know about only counting up to the 11th variable, and that is intended because there are only 11 integers between 2-12, a case 1 does not exist because if you throw 2 dice at once you'll not get 1 as a result.
I was more thinking about the loop only incrementing i up to 11. You want it to go through all values in the throw array don't you?

1
2
for(i=0;i<11;i++)
          ^^


Another thing you should also be aware of is that throwing two dice will be more likely to give you numbers in the middle of the range. 7 is most likely. 2 and 12 are the least likely. With your way of generating random numbers all numbers 2-12 have the same probability which makes it a poor simulation for throwing two dice.
Last edited on
Mathematically, rand() % 11 + 2 is not sound. It presumes that all values are equally likely. However, there are six possible ways to get a total of 7 ( 1,6 2,5 3,4 4,3 5,2 6,1) but only one way to get a total of 2 or 12.
http://mathforum.org/library/drmath/view/56688.html
Well suggested by the fact that this is posted in the 'beginners' thread, i'm not at a level where i could create an accurate "simulation", that is jsut the word that came to my mind at the time.


 
for(i=0;i=11;i++)


Yeah that part is my fault too. Not while writing, the code but while copy pasting. When i was editing the integers' and arrays' names I accidentaly screwed up that line and rewrote it from memory, the actual code does go up to 100. Again i'll edit it in a sec
It's very easy to improve. All you need to do is generate two random numbers in the range 1-6 and calculate the sum.
Topic archived. No new replies allowed.