In: Computer Science
DO IN C++
Write a function that will generate an array of random numbers. It needs to:
Take 3 integers as parameters
-The first is the minimum value
-the second is the maximum value
-the third is the size of the new array
-create a dynamically allocated array of the correct size
-generate a random number (between min and max) for each element in the array
-return a pointer to the array
Create a main() function that tests the above function and displays the values in the random array.
C++ Program:
/* C++ Program that generates an array of random numbers */
#include
#include
using namespace std;
//Function prototype
int* generate(int, int, int);
//Main function
int main()
{
//Pointer array
int* randomArray;
int i;
//Initializing random value
srand(time(NULL));
//Generating random array and storing
result
randomArray = generate(6, 23, 10);
cout << "\n\n Values in the Array: \n";
//Printing elements in the random array
for(i=0; i<10; i++)
cout << " \t "
<< randomArray[i];
cout << endl << endl;
return 0;
}
//Function that generates an array of random values
int* generate(int min, int max, int size)
{
int* arr;
int i;
//Allocating memory
arr = new int[size];
//Generating a random number and storing
value in the array
for(i=0; i
//Returning pointer array
return arr;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output: