
Patrick B. answered 08/10/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <math.h>
#include <string.h>
#include <cfloat>
#include <limits.h>
//calculator.h
#ifndef _CALCULATOR
#define _CALCULATOR
typedef struct _TStats
{
int N;
double minVal;
double maxVal;
double meanAvg;
double sumTotal;
double variance;
double median;
double skew;
} * TStats;
#define STATS_SIZE (sizeof(struct _TStats))
typedef
class Calculator
{
protected:
bool error_flag;
public:
Calculator()
{
error_flag=false;
}
bool GetError() { return(error_flag); }
void ClearError() { error_flag=false; }
double Add(double x , double y) { ClearError(); return(x+y); }
double Subtract( double x, double y) { ClearError(); return(x-y); }
double Multiply (double x, double y) { ClearError(); return(x*y); }
double Divide(double x, double y);
long Divide(long x, long y);
long Mod(long x,long y);
double PercentOf( double principal, double percent) { ClearError(); return(principal*percent/100); }
double PercentChange(double oldValue, double newValue);
double PercentIncrease(double principal,double percent) { ClearError(); return( principal*(1+percent/100)); }
double PercentDecrease(double principal,double percent){ ClearError(); return( principal*(1-percent/100)); }
double FutureValue(double principal, double percent, int n) { ClearError(); return( principal* pow((1+percent/100),n)); }
void Stats(double * a, TStats stats );
double zScore(double x, TStats stats) { ClearError(); return( (x-stats->meanAvg)/sqrt(stats->variance)); }
} * TCalculator;
#define CALCULATOR_SIZE (sizeof(Calculator))
#endif
double Calculator::Divide(double x, double y)
{
ClearError();
double retval;
if (y!=0)
{
retval = x/y;
}
else
{
retval = DBL_MAX;
error_flag=true;
}
return retval;
}
long Calculator::Divide(long x, long y)
{
ClearError();
long retval;
if (y!=0)
{
retval = x/y;
}
else
{
retval = LONG_MAX;
error_flag=true;
}
return retval;
}
long Calculator::Mod(long x,long y)
{
ClearError();
long retval;
if (y!=0)
{
retval = x%y;
}
else
{
retval = LONG_MAX;
error_flag=true;
}
return retval;
}
double Calculator::PercentChange(double oldValue, double newValue)
{
ClearError();
double retval;
if (newValue!=0)
{
retval = Divide((newValue-oldValue)*100,oldValue);
}
else
{
error_flag=true;
retval = DBL_MAX;;
}
return retval;
}
void Calculator::Stats(double * a, TStats stats )
{
ClearError();
if (stats->N>1)
{
double sumXSqr=0; // specifically, a[0]^2+ a[1]^2 + ... + a[N-1]^2
stats->sumTotal=0;
stats->maxVal = a[0];
stats->minVal = a[0];
for (int iLoop=0; iLoop<stats->N; iLoop++)
{
stats->sumTotal += a[iLoop];
sumXSqr += a[iLoop]*a[iLoop];
if (a[iLoop]>stats->maxVal) { stats->maxVal = a[iLoop]; }
if (a[iLoop]<stats->minVal) { stats->minVal = a[iLoop]; }
}
stats->meanAvg = stats->sumTotal/stats->N;
stats->variance = (sumXSqr - stats->N * stats->meanAvg*stats->meanAvg)/(stats->N-1);
int medianIndexPos = stats->N/2;
if ((stats->N%2)==0)
{
stats->median = (a[medianIndexPos]+a[medianIndexPos-1])/2;
}
else
{
stats->median = a[medianIndexPos];
}
stats->skew = (stats->meanAvg-stats->median)/stats->N;
}
else
{
memset(stats,0,STATS_SIZE);
error_flag=true; // division by zero on meanAvg if N=0 ;division by zero on variance if N=1
}
}
Go()
{
Calculator myCalculator;
double a[] = { 1.0, 2.5, 5,7.5};
struct _TStats stats;
stats.N=4;
myCalculator.Stats(a,&stats);
cout << stats.maxVal << endl;
cout << stats.minVal << endl;
cout << stats.meanAvg << endl;
cout << stats.median << endl;
cout << stats.variance << endl;
cout << stats.skew << endl;
cout << myCalculator.GetError() << endl;
// put your code here
}
int main()
{
Go();
}