(C++) Write a function that takes as input parameters (using call by pointer) 3 integers. It generates a random number between 25 and 50 (not including 50). It then creates an array on the memory heap of that length. It generates a random high number between 5 and 10 and a random low number between -5 and -10 and fills in the array iteratively with random numbers between the high and the low numbers*, and it returns that array. The input parameters should be modified so that it holds the length of the array, the high value, and the low value. In main, call the function 5 to print out the array. *not including the high – in general when we specify a range, we include the first value but not the last. If I forget to say that in the future, you can assume that’s what I intended.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int* randArray(int size){ int* arr = new int{size}; for(int i = 0;i<size;i++){ if(rand()%2==0){ arr[i] = 5+rand()%6; } else{ arr[i] = -10+rand()%6; } } return arr; } int main(){ int* res = randArray(5); for(int i = 0;i<5;i++){ cout<<res[i]<<" "; } cout<<endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.