int TwoThrows();
int main(){
int Throws, Throw, Frequency[13]={0,0,0,0,0,0,0,0,0,0,0,0,0};
randomize();
cout << "\nThis program simulates throws of two dice.";
cout << "\n\nHow many throws : ";
cin >> Throws;
// Calls TwoThrows and saves in Frequency by value
for(int I=0; I<Throws; I++){
Throw=TwoThrows(); //2-12
Frequency[Throw]++; //2-12
}
// Prints array:
for(int I=0; I<11; I++){
cout << I+2 << ":\t" << Frequency[I+2] << "\n";
}
return 0;
}
int TwoThrows(){
unsigned int I=(random(6)+1)+(random(6)+1);
return I;
}
这打印:
2:1317 3:2724 4:4145 5:5513 6:7056 7:8343 8:6982 9:5580 10:4176 11:2776 12:1388
这是伟大的。
但是,我想知道的是,为什么我必须将数组设置为 {0,0,0,0,0,0,0,0,0,0,0,0,0}?
如果我不这样做;我得到:
2:30626868 3:1638233 4:844545295 5:1 6:9 7:4202510 8:4199197 9:844555757 10:3 11:4202574 12:2130567168