In: Computer Science
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?).
// This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() << endl; cout << rand() << endl; cout << rand() << endl; return 0; }
C++ program that prompts user minimum and maximum value from user keyword. Then , print the three random numbers in a range of minimum and maximum values.
//main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//start of main function
int main()
{
// Get the system time.
unsigned seed = time(0);
//declare variables of integer type
int minimum;
int maximum;
// Seed the random number generator.
srand(seed);
cout<<"Enter minimum value :";
//read minimum value
cin>>minimum;
cout<<"Enter maximum value :";
//read maximum value
cin>>maximum;
// Display three random numbers.
cout<<"Random numbers"<<endl;
cout << rand()%(maximum-minimum)+minimum << endl;
cout << rand()%(maximum-minimum)+minimum << endl;
cout << rand()%(maximum-minimum)+minimum<< endl;
//pause program output console
system("pause");
return 0;
}//end of main function
Sample output screenshot: