Question
Write a function, called createNewArray, that creates an array of any size and randomly fills it with numbers between two values. Function outputs: a pointer
Write a function, called createNewArray, that creates an array of any size and randomly fills it with numbers between two values.
Function outputs: a pointer to a newly created array Function inputs: the size of the array, a seed value to randomly seed the random number generator, the minimum array value, the maximum array value.
Your program should read in all inputs from the user (provided code), call your function, and print out the new array (in main). REMINDER: If you dynamically allocate any memory, you MUST free that memory before the end of your program.
To generate random number, you will need the srand( seedValue ) function, where seedValue is an integer number, and rand() function which returns an integer number. Both functions are accessible in the stdlib.h header file. Your function MUST contain the two following lines of code. They will NOT appear next to each other. The first line will only need to be called once. The second line will be called every time a new value needs to be put into the array.
srand( seed ); // seeds the random number generator - tells it where to start grabbing numbers variableToStoreValue = rand()%rangeOfNumbers + minimumValue; // returns a random number that can be stored within an array
USE THE TEMPLATE PROVIDED BELOW
#include
// WRITE FUNCTION HERE. Your function must be called createNewArray int * createNewArray( int SIZE, int seed, int min, int max ) { // Dynamically allocate memory for an array // store a random value into each element of your array }
int main( void ){ // read in size of array from user, min, max, and seed int len, seed, minValue, maxValue; printf("Array size: "); scanf("%d", &len); printf("Minimum array value: "); scanf("%d", &minValue); printf("Maximum array value: "); scanf("%d", &maxValue); printf("Seed: "); scanf("%d", &seed); // call function int * newArray = createNewArray( len, seed, minValue, maxValue ); // PRINT OUT NEW ARRAY HERE // FREE MEMORY HERE return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started