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?
#include <iostream>
#include <ctime>
usingnamespace std;
int main()
{
srand((unsignedint)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";
elseif (evens < 50)cout << "There were more odd throws\n";
elseif (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;
}
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.
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.
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