Question

In: Computer Science

Write a program that calculates the mean, median and mode of a given array in the...

Write a program that calculates the mean, median and mode of a given array in the following manner:

  1. i) Write three functions mean (), median () and mode () that calculates the mean, median and mode of an array.

  2. 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.

  3. 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”.

Solutions

Expert Solution

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:


Related Solutions

Mean , Median and Mode
The mean of 8,11,6,14,m and 13 is 11 a) Find the value of m. b) Find the median and mode of given data set.
Create global variables Mean, Mode, Median, then create a method that takes an array of 10...
Create global variables Mean, Mode, Median, then create a method that takes an array of 10 double numbers and return three answers of the Mean, Median, Mode ( no need for implementation of the mean, median and mode, calculate them manually and return the answers), assign the answers to the global variables. In java, please!!
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data....
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data. An experiment was conducted to determine whether a deficiency of carbon dioxide in the soil affects the phenotype of peas. Listed below are the phenotype codes where 1 equals smooth dash yellow1=smooth-yellow​, 2 equals smooth dash green2=smooth-green​, 3 equals wrinkled dash yellow3=wrinkled-yellow​, and 4 equals wrinkled dash green4=wrinkled-green. Do the results make​ sense? 2 1 1 4 1 1 3 4 2 2 2...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data....
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data. An experiment was conducted to determine whether a deficiency of carbon dioxide in the soil affects the phenotype of peas. Listed below are the phenotype codes where 1 equals smooth dash yellow​, 2 equals smooth dash green​, 3 equals wrinkled dash yellow​, and 4 equals wrinkled dash green. Do the results make​ sense? 2 2 1 4 3 1 4 1 4 2 1...
1. Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample...
1. Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data. An experiment was conducted to determine whether a deficiency of carbon dioxide in the soil affects the phenotype of peas. Listed below are the phenotype codes where 1=smooth-yellow​,2=smooth-green​, 3=wrinkled-yellow​, and 4=wrinkled-green. Do the results make​ sense? 3 1 3 4 4 1 2 4 1 4 3 3 3 3 ​(a) The mean phenotype code is _____. 2. Statistics are sometimes used to...
Find the mean, median, and mode of the following set of data. (Enter solutions for mode...
Find the mean, median, and mode of the following set of data. (Enter solutions for mode from smallest to largest. If there are any unused answer boxes, enter NONE in the last boxes.) (a) 6 6 7 8 9 12 Mean Median Mode Mode (b) 6 6 7 8 9 108 Mean Median Mode Mode
Write short notes on Harmonic Mean, Arithmetic Mean, Geometric Mean, Median, Mode, Range, Standard Deviation, Interquartile...
Write short notes on Harmonic Mean, Arithmetic Mean, Geometric Mean, Median, Mode, Range, Standard Deviation, Interquartile Deviation, Skewness, Kurtosis and using practical examples, explain how these measures are useful in the field of Business Administration. (10marks)
how do i interpret mean, mode and median?
how do i interpret mean, mode and median?
1. Find the mean, median, mode, population standard deviation and variance of the given data: Items:...
1. Find the mean, median, mode, population standard deviation and variance of the given data: Items: 3,5,6,9,10,12,15 Frequency: 1,4,2,12,5,4,2 2.Find the mean, median, mode, sample standard deviation and variance of the discrete frequency distribution: Items: 2,5,6,7,12 Frequency:1,3,10,4,2
Why Mean, Median and Mode are called centrality measurements? Provide an example in which the Median...
Why Mean, Median and Mode are called centrality measurements? Provide an example in which the Median is a better indicator than the Mean Why Range, Mean Average Deviation, Variance and Standard Deviation are called Dispersion Measurements
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT