Write a function that will generate an array of random numbers. It needs to: tak
ID: 3790304 • Letter: W
Question
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.
Do not use array notation inside the function. Use pointer notation only. Do not use any type of operation such as cstdlib.h or ctime.h
This is C++. Please make sure the answer is readable and executable. Thanks
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
int* generatearray(int min ,int max,int no)
{
int * foo;
foo = new int [no];
for(int i=0;i<no;i++)
{
*(foo+i) = rand()%(max-min + 1) + min;
}
return foo;
}
int main()
{
int *a=generatearray(1,100,10);
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.