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
|
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
void readIn(int& n, double data[])
{
ifstream fin;
fin.open("data.txt.");
int i=0;
while (fin >> data[i])
{
fin >> data[i];
i++;
}
n = i + 1;
fin.close();
}
double calcMean(int n, double data[])
{
double sum, mean;
int i;
sum = 0;
for(i=0; i<n; i++)
{
sum = sum + data[i];
}
mean = sum / n;
return mean;
}
double calcDev(int n, double data[], double mean)
{
int i;
double stDev;
stDev = 0;
for(i=0; i<n; i++)
{
stDev = stDev + (data[i] - mean)*(data[i] - mean);
}
return stDev;
}
double standardDev(int n, double stDev)
{
double stdDev;
stdDev = sqrt(stDev / n);
return stdDev;
}
double calcLargest(int n, double data[])
{
int i;
double X;
X = data[0];
for(i=1; i<=n; i++)
{
if( X < data[i])
X = data[i];
}
return X;
}
double calcSmallest(int n, double data[])
{
int i;
double Y;
Y = data[0];
for(i=1; i <=n; i++)
{
if (Y > data[i])
Y = data[i];
}
return Y;
}
int main()
{
int n;
double data[2000], mean, stDev, stdDev, largest, smallest;
readIn(n, data);
mean = calcMean(n, data);
stDev = calcDev(n, data, mean);
stdDev = standardDev(n, stDev);
largest = calcLargest(n, data);
smallest = calcSmallest(n, data);
cout << "The largest value is " << largest << ".\n";
cout << "The smallest value is " << smallest << ".\n";
cout << "The mean value of these data is " << mean << ".\n";
cout << "The standard deviation of these data is " << stdDev << ".\n";
}
|