/* testarr07.cpp CIS 150 06/19/2008 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::cin; using std::cout; using std::setw; void cont(); int main() { 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(0)); // 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'; } cont(); return 0; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: 1: 19 2: 10 3: 20 4: 12 5: 19 6: 20 */