In: Computer Science
2. Write a program to do the following: • ask the user to input the size of an array of integers and allocate space for the array • ask the user to input the integer values for the array and store these values into the array • calculate and display the sum and the average of elements of the array
Program :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr,i,n,sum=0;
float avg;
printf("Enter the size of the array:");
scanf("%d",&n);
arr=(int*)calloc(n,sizeof(int));
printf("Enter %d values\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
sum=sum+arr[i];
printf("Sum of the array is: %d",sum);
avg=sum/n;
printf("\nAverage of the elements of the array is : %d",avg);
return 0;
}
Screen shot of the program with out put: