1. Engineering
2. Computer Science
3. this code calculates mean median and mode it is written...

# Question: this code calculates mean median and mode it is written...

###### Question details

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<iostream>
#include<math.h>

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<s; i++){
dataArray[i] = a[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<<endl<<"How many numbers do you want to enter:";
cin>>s;
cout<<endl;
cout<<"*********************************"<<endl;
double d[s];
for(int i=0;i<s;i++)
{
cout<<"Enter "<<i+1<<"th number:";
cin>>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<<endl;
cout << "Median is = ";
median.print();
cout << endl;
return 0;
}