我想查看两个滚动骰子的总和出现的次数。我正在写下面的代码向我展示与myArray的总和:计算两个骰子的总和出现的次数
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand (time(0));
int myArray[12];
for (int i =1; i < 10; i++)
{
int die_1 = rand() % 6 + 1;
int die_2 = rand() % 6 + 1;
myArray[(die_1 + die_2 - 2)]++;
cout<<endl<<"die_1 "<<die_1<<" die_2 "<<die_2<<" the sum: "<<die_1+die_2;
}
for (int i = 0; i < 12; i++)
cout<<endl<<myArray[i]<<" ";
}
但是代码向我返回奇怪的结果。希望有任何帮助来纠正它。
请记住,数组myArray中的值未初始化为零。它们将包含堆栈中内存中的任何随机数据。 – drescherjm 2015-02-07 15:28:56
'int myArray [12] = {0};' – P0W 2015-02-07 15:36:59
另外[不要使用'rand()'](http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful)。 – 2015-02-07 18:44:35