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)
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...
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 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
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...
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
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...
Write a function called randFill that fills the entries of an array with random integers in...
Write a function called randFill that fills the entries of an array with random integers in the range from 10 to 99 (inclusive). (You should use a standard Java method to generate the values. Your solution should use no more than 6 lines of code.) For example, a program that uses the function randFill follows. public class P4 { public static void main(String args[]) { int x[]; x = randFill(5); for (int i = 0; i < 5; i++) System.out.print(x[i]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT