Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in C++ and explain how the pointers are used Write a function that takes as

ID: 3870845 • Letter: C

Question

Code in C++ and explain how the pointers are used

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 (mine was 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.

*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.

Explanation / Answer

void calc(int *size,int *low, int *high) // FUNCTION CALC ACCEPTS THREE ARGUMENTS, SIZE, LOW,HIGH

{

std::random_device rd1; // only used once to initialise (seed) engine

std::mt19937 rng1(rd1()); // random-number engine used (Mersenne-Twister in this case)

std::uniform_int_distribution<int> uni(25,50);// guaranteed unbiased

int length = uni(rng1); //Generating a random number between 25 and 50

int *myArray = new int[length]; // Creating an array myArray of length "length" on the heap memory

cout<<length<<" ";

std::random_device rd2; // only used once to initialise (seed) engine

std::mt19937 rng2(rd2()); // random-number engine used (Mersenne-Twister in this case)

std::uniform_int_distribution<int> num(*low,*high);// low and high are lower and higher limits (parameters)

for(int i=0; i<length;i++)

{

myArray[i] = num(rng2); //Generate the value of the elements of the array

}

int lowest= myArray[0];

int highest=myArray[0];

for(int i=0; i<length;i++) // for loop to calculate the highest and the lowest value in the array

{

myArray[i] = num(rng2);

if(myArray[i]>highest)

highest=myArray[i];

if(myArray[i]<lowest)

lowest=myArray[i];

}

*low= lowest; //Modifying the lowest value

*high= highest; //Modifying the highest value

*size= length; // Modifying the array size

}