/* testarr08.cpp CIS 150 06/23/2005 David Klick Using an array as a set of accumulators. This program sums up the results of double dice rolls. */ #include #include #include #include using std::cout; using std::setw; int main(void) { int i; int die; int rolls[13]; // array to hold results of rolls // array has two extra elements because we want // roll[2] through roll[12], but we will // automatically get roll[0] and roll[1] // as well // initialize random number generator srand(time(NULL)); // initialize array to 0s (the elements are accumulators) for (i=0; i<13; i++) { rolls[i] = 0; } // generate 1000 rolls and tally them for (i=0; i<1000; i++) { die = rand() % 6 + rand() % 6 + 2; // simulate 2 dice rolling rolls[die]++; // add 1 to the corresponding accumulator } // display results for (i=2; i<13; i++) { cout << setw(2) << i << ": " << setw(3) << rolls[i] << '\n'; } return 0; }