/* testarr07.cpp CIS 150 06/23/2005 David Klick Using an array as a set of accumulators. This program sums up the results of die rolls. */ #include #include #include #include using std::cout; using std::setw; int main(void) { int i; int die; int rolls[7]; // array to hold results of rolls // array has one extra element because we want // roll[1] through roll[6], but all arrays // start with index 0 // initialize random number generator srand(time(NULL)); // initialize array to 0s (the elements are accumulators) for (i=0; i<7; i++) { rolls[i] = 0; } // generate 100 rolls and tally them for (i=0; i<100; i++) { die = rand() % 6 + 1; // simulate die roll rolls[die]++; // add 1 to the corresponding accumulator } // display results for (i=1; i<7; i++) { cout << i << ": " << setw(2) << rolls[i] << '\n'; } return 0; }