Question

In: Computer Science

Write a function that will generate an array of random numbers. It needs to:

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.


Solutions

Expert Solution

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         *(arr+i) = min + rand() % (max - min + 1);

    //Returning pointer array
    return arr;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:


Related Solutions

1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
Write the R code to generate five independent uniform random numbers and use that to generate...
Write the R code to generate five independent uniform random numbers and use that to generate 5 independent Bernoulli random variables with probability of success p = 0.4. please use RStudio. please do not use lab nor rbern for calculations.
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
How to inhabit the int array num with random numbers using a function that you then...
How to inhabit the int array num with random numbers using a function that you then invoke from the main method. I already created a function that generates an array of 5 random integers. /* int i;   int arrayName[5];     for(i= 0;i < 6; i++ ){            //type arrayName[arraysize];            arrayName[i] = rand()% 200;       printf("array[%d] = %d\n",i,arrayName[i]); */ // use rand() % some_number to generate a random number int num[5]; int main(int argc, char **argv) { // hint // function invocation goes here...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
The language is MATLAB Write a function that will generate three random integers, each in the...
The language is MATLAB Write a function that will generate three random integers, each in the inclusive range from 10 to 80. It will then return a string consisting of the three integers joined together, and also a character vector consisting of the three integers joined together. For example, if the random integers are 11, 29, and 76, the string that is returned will be "112976" and the character vector that is returned will be '112976'. I'm really confused on...
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT