- Your Job Is To Implement Two Functions 1 Int Scan Array Int Size Which Should Scan In Size Integers And Store Them 1 (156.05 KiB) Viewed 46 times
Your job is to implement two functions: 1. int *scan_array(int size);, which should scan in size integers and store them
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Your job is to implement two functions: 1. int *scan_array(int size);, which should scan in size integers and store them
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: 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: 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