In: Computer Science
Write a program that calculates the mean, median and mode of a given array in the following manner:
i) Write three functions mean (), median () and mode () that calculates the mean, median and mode of an array.
ii) Using these functions write a program that calculates all the three above mentioned values for two different arrays A and B where A is an array that contains the number of hours spent by a person each day for playing video games and B is an array that contains the number of hours spent by a person each day for his studies.
iii) Based on the above mentioned values that you calculate using these functions compare the values obtained from both the arrays. If all the three values of array A is greater than array B
print statement that warns the person to study. If all the three values of array B is greater than array A print statement that praises him for his studies. If all the three values are equal print
statement that tells him he is well balanced. If any other issues, print “Sorry can’t come to an outcome”.
(#PLEASE NOTE: The output should exactly look like the example below.....coding required in "C")
Example:
Let array A be: 4 5 5 2 8 9 Let array B be: 2 3 1 4 8 4
Mean of A is: 5.5 Mean of B is: 3.6
Median of A is: (5+2)/2=3.5
Median of B is: (1+4)/2=2.5
Mode of A is: 5 Mode of B is: 4
Here Mean, Median and Mode of A is larger than B.
Therefore we print “You’re playing more video games you have to spent more time studying”.
In case if Mean, Median and Mode of A is lesser than B then you print “Good, you’re spending considerable time in studying”.
Note: We must sort the array before calculating the median, so in the below c code, the median is calculated after sorting the array. If you do not want to sort the array then comment the sort function in the median function.
Desired C program:
#include<stdio.h>
// this function sort the array using bubble sort algorithm
void sort(int arr[],int len)
{
int i, j;
for (i = 0; i < len-1; i++)
for (j = 0; j < len-i-1; j++)
if (arr[j] > arr[j+1])
{
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
// mean function take array and length of array as input and return mean of array
float mean(int arr[], int len)
{
int sum = 0;
for(int i=0;i<len;i++)
{
sum+= arr[i];
}
return (float)sum/(float)len;
}
// median function take array and length of array as input and return median of array
float median(int arr[],int len)
{
// we must sort the array before calculating the median
sort(arr,len);
// if length is odd then there is only one element in middle of array
if(len%2==1)
return (float)arr[len/2];
// if length is even then median will be average of middle two elements
else
{
printf("(%d + %d)/2 = ",arr[len/2 -1],arr[len/2]);
return (float)(arr[len/2]+arr[len/2 -1 ])/ 2.0 ;
}
}
//mode function take array and length of array as input and return mode of array
int mode(int arr[],int len)
{
// sort the array so that we can find out which element's frequency is maximum in array
sort(arr,len);
// result stores the element having maximum frequency and we intialize if with first element of arr
int result = arr[0];
// max_mode_count stores frequency of result and initially its value is 1 as result contains first element of arr initially
int max_mode_count = 1;
// temp_mode and temp_count stores temporay element and its frequency
int temp_mode = arr[0];
int temp_count = 1;
// now iterate through each element of arr and update result and max_mode_frequency accordingly
for(int i=1;i<len;i++)
{
if(arr[i] == temp_mode)
{
temp_count += 1;
if(max_mode_count < temp_count)
{
result = temp_mode;
max_mode_count = temp_count;
}
}
else
{
temp_mode = arr[i];
temp_count = 1;
}
}
return result;
}
// main function to drive the code
int main()
{
int A[] = {4,5,5,2,8,9};
int B[] = {2,3,1,4,8,4};
int lenA = (int)(sizeof(A)/sizeof(A[0]));
int lenB = (int)(sizeof(B)/sizeof(B[0]));
printf("Let array A be : ");
for(int i=0;i< lenA ;i++)
printf("%d ",A[i]);
printf("Let array B be : ");
for(int i=0;i< lenB ;i++)
printf("%d ",B[i]);
printf("\n");
printf("Mean of array A is : ");
float meanA = mean(A,lenA);
printf("%.1f ",meanA);
printf("Mean of array B is : ");
float meanB = mean(B,lenB);
printf("%.1f \n",meanB);
printf("Median of array A is : ");
float medianA = median(A,lenA);
printf("%.1f ",medianA);
printf("Median of array B is : ");
float medianB = median(B,lenB);
printf("%.1f \n",medianB);
printf("Mode of array A is : ");
int modeA = mode(A,lenA);
printf("%d ",modeA);
printf("Mode of array B is : ");
int modeB = mode(B,lenB);
printf("%d",modeB);
printf("\n");
if(meanA > meanB && medianA > medianB && modeA > modeB)
{
printf("You're playing more video games you have to spent more time studying \n");
}
else if(meanA < meanB && medianA < medianB && modeA < modeB )
{
printf("Good, you’re spending considerable time in studying \n");
}
else
{
printf("Sorry can’t come to an outcome \n");
}
return 0;
}
Output: