#include using namespace std; #include "dice.h" // simulate rolling two dice to obtain all possible sums // repeat the "experiment" specified number of times // Owen Astrachan, 8/9/94, modified 6/9/95, 4/20/99 double RollTest(int target,int experiments); int main() { int numTimes; // for one trial long totalRolls; // accumulate for all trials int k; cout << "Please enter number of trials: "; cin >> numTimes; totalRolls = 0; for(k=2; k <= 12; k++) { cout << k << "\t" << RollTest(k,numTimes) << endl; } return 0; } double RollTest(int target, int trials) // precondition: 2 <= target <= 12, 0 < trials // postcondition: returns average # of rolls needed to obtain target // trying 'trials' times { Dice d1(6); Dice d2(6); int total = 0; int k; for(k=0; k < trials; k++) { int numRolls = 1; //first time through loop is 1 roll while (d1.Roll() + d2.Roll() != target) { numRolls += 1; } total += numRolls; } return (double(total) / trials); }