/* testarr08.cpp CIS 150 06/19/2008 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::cin; using std::cout; using std::setw; void cont(); int main() { 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(0)); // 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'; } 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: 2: 24 3: 48 4: 80 5: 125 6: 132 7: 163 8: 141 9: 110 10: 86 11: 58 12: 33 */