EXERCISE 00): Working with malloc'd arrays Complete the C program, mallocd_array.c. Download mallocd_array.c here, or co
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
EXERCISE 00): Working with malloc'd arrays Complete the C program, mallocd_array.c. Download mallocd_array.c here, or co
EXERCISE 00): Working with malloc'd arrays 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 . DANGER: 1 2 3 4 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. HINT: 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. The output from your program should look exactly like this: ./mallocd_array Enter size: 5 Enter 5 integers:
The output from your program should look exactly like this: ./mallocd_array Enter size: 5 Enter 5 integers: 1 2 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: 5 -4 -3 The average of all values in the array is: -0.67 Assumptions/Restrictions/Hints • You do not need to handle memory leaks for this exercise. i.e. You should not call free • All inputs be of the correct type