Complete the following program that dynamically allocates memory for a 1-D array, allows the
user to input values, and then performs some basic operations on the array.
Instructions:
1. Prompt the user to enter the number of elements in the array.
2. Dynamically allocate memory for the array using malloc.
3. Prompt the user to enter values for each element in the array one-by-one.
4. Perform the following operations:
Calculate and print the sum of all elements in the array.
Calculate and print the average of the elements.
Find and print the maximum and minimum values in the array.
5. Free the allocated memory after use.
Assumptions:
The user always enters the desired type of data.
The program only needs to performs the required operations once and then exits.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i; // n stores the number of elements; i is loop variable
int *array; // arr points to the 1-D array
int sum = 0;
int max, min;
double average;
// Step 1: Prompt the user to enter the number of elements
printf ("Enter the number of elements: ");
// Your code goes here.
// Step 2: Dynamically allocate memory for the array
// Your code goes here.
// Step 3: Allow the user to enter values for each element in the
array one-by-one
printf ("Enter %d elements: \n", n);
// Your code goes here.
// Step 4: Perform the operations
// Your code goes here.
// Print the results
printf("Sum: %d\n", sum);
printf ("Average: %.2f\n", average);
}
printf ("Maximum: %d\n", max);
printf ("Minimum: %d\n", min);
// Step 5: Free the allocated memory
// Your code goes here.
return 0;