Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3588724 • Letter: W

Question

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

Explanation / Answer

#include<iostream>
#include<math.h>

void sort(int a[], int n){
   int temp;
   for (int i = 0; i<n; i++){
      for (int j = i+1; j<n; j++)
          if (a[i] > a[j]){
              temp = a[i];
              a[i] = a[j];
              a[j] = temp;
          }
   }
}


double mean(int a[],int n){
    double sum;
    sum = 0;
    for (int i = 0; i<n; i++){
         sum = sum + a[i];
    }
    return sum/n;
}
double median(int a[],int n){
     sort(a,n);
     if (n % 2 == 0){
        return ((double)a[n/2] + (double)a[(n/2)-1])/2;
     }
     else {
          return (a[n/2]);
     }

}
int mode(int a[], int n){
sort(a,4);
int maxcount = 0;
int max = 0;
for (int i = 0; i<n; i++){
     int count = 0;
     for (int j = i; j<n; j++){
        if (a[j] == a[i]){
           count++;
        }
     }
     if (count > maxcount){
         maxcount = count;
         max = a[i];
     }
}
return max;

}

using namespace std;

int main(){
   int n;


   cout << "Enter number of data items:";
   cin >> n;
   int *a = new int[n];
   int *b = new int[n];
   cout << "Enter four " << n << " values for hours spent in video games:";
   for (int i = 0; i<n; i++)
       cin >> a[i];
   cout << "Enter four values for hours spent in studies:";
   for (int i = 0; i<n; i++)
       cin >> b[i];


  
   double meana = mean(a,n);
   double meanb = mean(b,n);
  
   double mediana = median(a,n);
   double medianb = median(b,n);

    int modea = mode(a,n);
    int modeb = mode(b,n);
    
   cout << "For games:" << endl;
   cout << "Mean:" << meana << endl;;
   cout << "Median:" << mediana<< endl;;
   cout << "Mode:" << modea << endl;
   cout << "For Studies:" << endl;
   cout << "Mean:" << meanb<< endl;;
   cout << "Median:" << medianb<< endl;;
   cout << "Mode:" << modeb<< endl;

   if (meana > meanb && mediana > medianb && modea > modeb){
         cout << "You should study more ";
   }
   else if (meana < meanb && mediana < medianb && modea < modeb){
         cout << "You are studying well ";
   }
   else if (meana == meanb && mediana == medianb && modea == modeb){
         cout << "You are well balanced ";
   }  
   else {
         cout << "Sorry can't come to an outcome ";
   }


   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote