Question: THIS CODE CALCULATES MEAN MEDIAN AND MODE IT IS WRITTEN IN C++ BUT IT IS NOT GIVING THE CORRECT MODE ! PLEASE FIX THE CODE

THIS CODE CALCULATES MEAN MEDIAN AND MODE IT IS WRITTEN IN C++ BUT IT IS NOT GIVING THE CORRECT MODE ! PLEASE FIX THE CODE TO GIVE THE CORRECT MODE .

CHECK THAT THE MODE IS CORRECT FOR THE FOLLOWING DATASETS: 110 13 120 116 180 49 150 110 100 132

-19 999 67 89 -112 78 101 -16 146 -82

AND SHOULD BE CORRECT FOR ANY OTHER DATA SET INPUT

#include #include

using namespace std;

class statistical{ protected: double *dataArray; int size; public: statistical(double a[], int s){ dataArray = new double[s]; for (int i = 0; i

size = s; } virtual void print() = 0; }; class Mean : public statistical{

public:

Mean(double a[]=0, int s=0) :statistical(a, s){

} double mean() { double Sum = dataArray[0]; for (int i = 1; i < size; ++i) { Sum += dataArray[i]; } return Sum / size; } void print(){ cout << mean(); } }; class Median : public statistical{ public: Median(double a[], int s) :statistical(a, s){ } void print(){ // Allocate an array of the same size and sort it. double* Sorted = new double[size]; for (int i = 0; i < size; ++i) { Sorted[i] = dataArray[i]; } for (int i = size - 1; i > 0; --i) { for (int j = 0; j < i; ++j) { if (Sorted[j] > Sorted[j + 1]) { double Temp = Sorted[j]; Sorted[j] = Sorted[j + 1]; Sorted[j + 1] = Temp; } } } // Middle or average of middle values in the sorted array. double Median = 0.0; if ((size % 2) == 0) { Median = (Sorted[size / 2] + Sorted[(size / 2) - 1]) / 2.0; } else { Median = Sorted[size / 2]; } delete[] Sorted; cout << Median; } }; class Mode : public statistical{ public: Mode(double a[], int s) :statistical(a, s){ } void print(){ int* ipRepetition = new int[size]; for (int i = 0; i < size; ++i) { ipRepetition[i] = 0; int j = 0; //bool bFound = false; while ((j < i) && (dataArray[i] != dataArray[j])) { if (dataArray[i] != dataArray[j]) { ++j; } } ++(ipRepetition[j]); } int iMaxRepeat = 0; int flag = 0; for (int i = 1; i < size; ++i) { if (ipRepetition[i] > ipRepetition[iMaxRepeat]) { iMaxRepeat = i; flag = 1; } }

if(flag == 1) cout << dataArray[iMaxRepeat]; else cout << "0"; delete[] ipRepetition; } };

int main() {

int s; cout<>s; cout<>d[i];

} Mean mean(d, s); cout << endl << "Mean is = "; mean.print(); Mode mode(d, s); cout << endl << "Mode is = "; mode.print(); Median median(d, s); cout<

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!