Question
THIS CODE CALCULATES THE MEAN MEDIAN AND MODE OF DATA GIVEN BY USER IT IS WRITTEN IN C++ . MODIFY IT SO THAT IT SENDS
THIS CODE CALCULATES THE MEAN MEDIAN AND MODE OF DATA GIVEN BY USER IT IS WRITTEN IN C++ . MODIFY IT SO THAT IT SENDS AN ERROR MESSAGE IF THE USER ENTERS ANYTHING OTHER THAN A NUMBER AND ASKS THEM TO RE-ENTER ANOTHER NUMBER IN ITS PLACE. THE CODE ASKS THE USER FOR NUMBERS IN MAIN() AND ASKS WITH THE HELP OF A FOR LOOP
#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
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])) { ++j; } ++(ipRepetition[j]); } int iMaxRepeat = 0; int flag = 0; for (int i = 0; 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<
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< There are 3 Steps involved in it See step-by-step solutions with expert insights and AI powered tools for academic successStep by Step Solution
Step: 1
Get Instant Access to Expert-Tailored Solutions
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started