Write a program in C to read in an array find its average an

Write a program in C to read in an array, find its average and then print the array and its average. What makes this assignment different is that you are going to use a dynamic array.

Make sure to have separate methods for reading input, calculating the average and writing the output. Make sure that the your input method prompts the users for the number of values being averaged, sets up a dynamic array of that size and then reads in the corresponding number of values.

Solution

#include <stdio.h>
#include <stdlib.h>

void inputData(int*,int);
void average(int*,int,float*);
void outputData(int*,int,float);
int main()
{
int n, *arr;
float avg;
printf(\"Enter the number of elements in array\ \");
scanf(\"%d\",&n);
arr=(int*)malloc(n*sizeof(int));
inputData(arr,n);
average(arr,n,&avg);
outputData(arr,n,avg);
}
void inputData(int *arr,int n)
{
for(int i=0;i<n;i++)
    {
      printf(\"Enter the %d number:\ \",i+1);
      scanf(\"%d\",&arr[i]);
    }
}
void average(int *arr,int n, float *avg)
{
int sum=0;
for(int i=0;i<n;i++)
    {
      sum+=arr[i];
    }
*avg=(float)sum/n;
}
void outputData(int *arr,int n,float avg)
{
printf(\"The contents of array are\ \");
for(int i=0;i<n;i++)
    {
      printf(\"%d\ \",arr[i]);
    }
printf(\"\ The average of the array is %f\ \",avg);
}

Write a program in C to read in an array, find its average and then print the array and its average. What makes this assignment different is that you are going

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site