//*********************************************************** // file: guess2.cpp // Author: // Create Date: // Last Modified: // Class: CPSC 220, section // Language: C++ // Purpose: Allows the user to guess a random number // Input: (from standard input) random number seed and // a guess // Output: (to standard output) the result of the guess // Pledged: // //*********************************************************** #include #include // prototype for function random. Returns a random integer with the // range specified by the parameters int random( int // lower value for range of random number , int // upper value for range of random number ); int main ( void ) { // Constant declarations const int UPPER_LIMIT = 10; // maximum value that can be produced // by the random number generator const int LOWER_LIMIT = 1; // minimum value that can be produced // by the random number generator // Local variable declarations int number; // the number to be guessed int guess; // the users guess int seed; // a seed for the random number generator // Get a seed for the pseudo-random number generator cout << "Let's play a guessing game." << endl; cout << "First, enter a number between 1 and 100 to act " << "as a seed for the random number genreator:"; cin >> seed; // Initialize the random number generator with the random seed srand(seed); // Generate a random number in the range [LOWER_LIMIT,UPPER_LIMIT] number = random(LOWER_LIMIT, UPPER_LIMIT); // Ask for a guess cout << "OK, now guess a number between 1 and 10:"; cin >> guess; // Tell the user whether the guess is right or wrong if (guess != number) cout << "Too bad. I was thinking of " << number << "." << endl; else cout << "Congratuations! " << guess << " was correct!" << endl; return 0; } // end main //******************************************************************** // function: int random (const int lowerLimit, const int upperLimit ) // Purpose: produces a pseudo-random number // Parameters: integer representing the minimum possible // value of the number generated // integer representing the maximum possible // value of the number generated // Postcondition: returns a pseudo-random number //******************************************************************** int random ( int lowerLimit, // min possible value of number generated int upperLimit ) // max possible value of number generated { int randomValue; // stores the random number generated randomValue = (rand( ) % upperLimit) + lowerLimit; return randomValue; } // end function Random()