In C 4 Write a function findMean that takes as an argument a
In C
4) Write a function “findMean” 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.
5) Write a function “findMode” that takes as an argument an integer array and an integer for the size of the array. It should return an integer of the mode of that array. If there are multiple modes, it can return any one of them.
Solution
-----------------------------ANSWER 4--------------------------------
#include <stdio.h>
#include <stdlib.h>
/* function declaration */
double findMean(int arr[], int size);
int main () {
/* integer array with 10 elements */
int arr[10] = {5,2,8,3,15,10,33,3,22,6};
double mean;
/* calling findMean function */
mean = findMean(arr,10);
printf( \"\ mean (average) of that array : %f \ \", mean );
return 0;
}
/* findMean function definition*/
double findMean(int arr[], int size)
{
double avg=0;
double sum=0;
int i;
for(i=0;i<size;i++)
{
sum=sum+arr[i];
}
avg=sum/10;
return avg;
}
---------------
output is:- mean (average) of that array : 10.700000
----------------------------------------------------ANSWER 5---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
/* function declaration */
int findMode(int arr[], int size);
int main () {
/* integer array with 10 elements */
int arr[10] = {5,2,8,3,15,10,33,3,22,6};
int mode;
/* calling findMode function */
mode=findMode(arr,10);
printf( \"\ mode of that array : %d \ \", mode );
return 0;
}
/* findMode function definition*/
int findMode(int arr[], int size)
{
int maximum_Value = 0;
int maximum_count = 0;
int i,j;
for (i = 0; i < size; i++)
{
int count = 0;
for (j = 0; j < size; j++)
{
if (arr[j] == arr[i])
count=count+1; /*if arr[j] == arr[i] then increase count*/
}
if (count > maximum_count ) {
maximum_count = count;
maximum_Value = arr[i];
}
}
return maximum_Value;
}
------------------
output is:- mode of that array : 3
=======================================================================
If you have any query, please feel free to ask
Thanks a lot

