In C: write a function \"Mean\" that takes as an argument an integer array and a
ID: 3925560 • Letter: I
Question
In C: write a function "Mean" that takes as an argument an integer array and an integer for the size of the array. It should return a double of the mean (average) of the values in the array. In C: write a function "Mean" that takes as an argument an integer array and an integer for the size of the array. It should return a double of the mean (average) of the values in the array. In C: write a function "Mean" that takes as an argument an integer array and an integer for the size of the array. It should return a double of the mean (average) of the values in the array.Explanation / Answer
Please find the required program along with its output. Please see the comments against each line to understand the step.
#include <stdio.h>
double mean(int *arr, int s){
double sum = 0; //initialize sum to 0
for(int i=0; i<s; i++){ //iterate through all the array elements
sum = sum + arr[i]; //get the total sum
}
double avg = sum/s; //get the average
return avg; //return average
}
int main()
{
int arr[6] = {1,4,6,2,7,9}; //delcare and initialize int array
double avg = mean(arr,6); //call the mean funtion to get the mean
printf("Mean = %lf",avg); //print the mean
return 0;
}
------------------------------------------------------
OUTPUT:
Mean = 4.833333