1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAXSIZE = 1000;
/**
* computes the maximum and minimum values in the data set
* @param data - an array containing the measurements
* @param count - the number of measurements
* @param min - the minimum measurement
* @param max - the maximum measurement
*/
void minMax(const double data [ ], int count, double& min, double& max)
{
min = data[0];
for(count = 0; count<MAXSIZE;)
{
count++;
}
if (data[count] < min)
{
min = data[count];
}
max = data[0];
for(count=0; count<MAXSIZE;)
{
count++;
}
if(data[count] > max)
{
max = data[count];
}
double range = max - min;
cout<<"Min: " <<setprecision(4)<<setw(20)<<min;
cout<<"Max: " <<setprecision(4)<<setw(20)<<max;
cout<<"range: " <<setprecision(4)<<setw(18)<<range;
/**
* computes the sample mean
* @param data - an array containing the measurements
* @param count - the number of measurements
* @return the mean of the set of measurements
*/
double calcMean(const double data[ ], int count)
{
double sum = 0;
double mean;
int i;
for(i = 0; i < count; i++);
{
sum += data[i];
mean = sum/count;
}
return mean;
}
/**
* computes the sample variance
* @param data - an array containing the measurements
* @param count - the number of measurements
* @return the sample variance of the set of measurements
*/
double calcVariance(const double data[ ], int count)
{
double sum = 0;
double var = 0;
double mean;
int i;
for(i = 0; i < count; i++);
{
sum += data[i];
mean = sum/count;
var = pow(data[i]-mean,2);
}
return var;
}
int main()
{
double data[MAXSIZE];
double range, mean, variance, stand_dev;
string file;
int count;
cout<<"Enter the name of the data file>"<<endl;
cin>>file;
cout<<endl;
cout<<"Descriptive Statistics"<<endl;
cout<<"------------------------------"<<endl;
ifstream inputFile;
inputFile.open("sewage.data");
if(!inputFile)
{
cout<<"error opening File."<<endl;
}
while(count = 0,count < MAXSIZE && inputFile >> data[count])
{
count++;
}
cout<<"N: "<<setw(22);
cin>>count;
minMax(data, count, min, max);
cout<<"range: " <<setprecision(4)<<setw(18);
mean = calcMean(data, count);
cout<<"mean:"<<setprecision(4)<<setw<<(19)<<mean<<endl;
variance = calcVariance(data, count);
cout<<"variance:"<<setw(15)<<setprecision<<(4)<<variance<<endl;
stand_dev = sqrt(variance);
cout<<"Standard Deviation: "<<setw(6)<<setprecision(4)<<stand_dev<<endl;
inputFile.close();
return 0;
}
|