// // Program name: roll_dice_v1 // // Input: none. // Output: (on standard output) how many rolls it takes to see all // 6 faces of a die, trying one random sequence of rolls. // #include #include // has EXIT_SUCCESS, rand() // Function prototypes. int roll(int numFaces); // Main program. int main() { const int FacesOnDie = 6; // total number of faces on die bool seen[FacesOnDie]; // seen[i] is true if we've see face // i, false otherwise. int rolls = 0; // number of rolls so far int numSeen = 0; // number of distinct faces so far int thisRoll; // face displayed by current roll // Initialize seen array. for (int i = 0; i < FacesOnDie; ++i) seen[i] = false; // Roll the die repeatedly until we've seen all faces. while (numSeen < FacesOnDie) { // "Roll the die": generate a random number between // 1 and FacesOnDie. thisRoll = 1 + roll(FacesOnDie); ++rolls; cout << "the number is " << thisRoll << endl; if (!seen[thisRoll-1]) { // We haven't seen this face before. seen[thisRoll-1] = true; ++numSeen; } } cout << endl << rolls << " rolls were required\n"; return EXIT_SUCCESS; } // Function definitions. // Function to "roll a die". // Pre: numFaces is positive. // Post: returns a random integer between 0 and numFaces-1. int roll(int numFaces) { return int(((double(numFaces) * rand())/(RAND_MAX + 1.0))); }