Complete the C program, mallocd_array.c. Download mallocd_array.c here, or copy it to your CSE account using the followi
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Complete the C program, mallocd_array.c. Download mallocd_array.c here, or copy it to your CSE account using the followi
//////////////// DO NOT CHANGE ANY OF THE CODE BELOW HERE//////////////////
int *scan_array(int size);double calculate_average(int *array, int size);
int main (void) {
int size; printf("Enter size: "); scanf(" %d", &size); if (size <= 0) { printf("Invalid Size\n"); return 1; } printf("Enter %d integers:\n", size); int *array = scan_array(size);
printf("The average of all values in the array is:%.2lf\n", calculate_average(array,size));
return 0;}//////////////// DO NOT CHANGE ANY OF THE CODE ABOVE HERE//////////////////
///////////////////////////////////////////////////////////////////////////////////////////////// ONLY WRITE CODE BELOW HERE///////////////////////////////////////////////////////////////////////////////////////////////////////
// A function which scans in `size` integers and stores theminto a // malloc'd array.// returns: a pointer to the malloc'd arrayint *scan_array(int size) { // TODO: 1. create a MALLOC'd array of size`size`
// TODO: 2. Loop through and scan values into thearray.
// TODO 3.delete the following line and return thearray. return NULL;}
// Given an integer array and it's size, // returns the sum of all elements inside the array, divided by thesize of the// array.double calculate_average(int *array, int size) { // TODO calculate the sum of the array.
return 0;}
///////////////////////////////////////////////////////////////////////////////////////////////// ONLY WRITE CODE ABOVE HERE///////////////////////////////////////////////////////////////////////////////////////////////////////
fill this code please
Complete the C program, mallocd_array.c. Download mallocd_array.c here, or copy it to your CSE account using the following command: /web/cs1511/22T2/activities/mallocd_array/mallocd_array.c. $ cp -n The main function has already been written for you. You must not modify it in any way. Your job is to implement two functions: 1. int *scan_array(int size);, which should scan in size integers and store them in a malloc'd array. It should then return the malloc'd array. 2. int calculate_average (int *array, int size);, which should calculate and return the average of all elements of array. 1 2 DANGER: You won't be able to complete this exercise without using the function malloc to create the array. If you attempt to create the array as a local variable e.g. int array[size]; and then return it, your code will likely crash and give you a headache. The output from your program should look exactly like this: ./mallocd_array Enter size: 5 Enter 5 integers: HINT: 5 To use malloc, you'll need to calculate how many bytes to allocate to your array. How might sizeof(int) help you with this? You'll also need to #include stdlib.h to be able to access the malloc function. 3 4 5 The average of all values in the array is: 3.00 ./mallocd_array Enter size: 1 Enter 1 integers: 10 The average of all values in the array is: 10.00 ./mallocd_array Enter size: 3 Enter 3 integers: -4 -3 The average of all values in the array is: -0.67