Question: array_alloc.c code : #include #include /* get_array -- allocates and initializes an array */ double *get_array(int size) { /* for now, just return NULL */

 array_alloc.c code : #include #include /* get_array -- allocates and initializes

array_alloc.c code :

#include #include

/* get_array -- allocates and initializes an array */ double *get_array(int size) { /* for now, just return NULL */ /* you should complete this function */

return NULL; }

/* print_array -- prints the elements of the array, unless null */ void print_array(double arr[], int length) { /* test for null; if not print array elements */ if (arr == NULL) { printf("(null array) "); } else { /* print the elements */ int i; for (i = 0; i

/* main program -- call getArray and print its results */ int main() { /* variable to hold the array */ double *the_array = NULL;

/* prompt user for the size */ int size = -1; while(size

if(scanf("%d", &size) != 1) { printf("Data not scanned well. Exiting. "); return EXIT_FAILURE; } }

/* call get_array to create an populate array; then print it */ the_array = get_array(size); if(the_array != NULL) { print_array(the_array, size); }

/* free memory */ free(the_array);

return EXIT_SUCCESS; }

Open and examine the file array alloc.c. The main function in array_alloc.c: prompts the user for a non-negative integer; re-prompts if necessary calls the get array function with the given argument, which returns (a pointer to) a dynamically-allocated array. If the result of get_array is non-null, the main function will assume that it is to an array of double whose length is the integer argument. prints the values in the array by calling the printArray function frees the memory allocated for the array; note that you should also free memory that is allocated on the heap when your program is done using that memory As implemented, the get array function simply returns null. Modify it so that it: checks the argument for being negative. If it is negative, it is treated as if the argument was 0 allocates an array of doubles of the given length (hint: use the calloc function or malloc function to do this) initializes the array so that for all values n in its range, the value at index n of the array is n * 10.0. Compile and run your program Checkpoint 2 [20 execution. oints]: S how your modifi ste text to th allo De rogram (take strate its

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!