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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
const int MAX=30;
void get_data(ifstream&,double[],int&);
double smallest(double[],int);
double largest(double[],int);
double average(double[],int);
double variance(double[],double,int);
double deviation(double);
void print_results(double,double,double,double,double,double[],int);
int main()
{
double list[MAX];
int n;
int factor;
ifstream in;
string fname;
double small,large,ave,var,stddev;
cout << "Please enter input file name" << endl;
cin >> fname;
in.open(fname.c_str());
get_data(in,list,n);
in.close();
small = smallest(list,n);
large = largest(list,n);
ave = average(list,n);
var = variance(list,ave,n);
stddev = deviation(var);
print_results(small,large,ave,var,stddev,factor,list,n);
return 0;
}
void get_data(ifstream& in,double list[],int& n)
{
n=0;
in >> list[n];
while (!in.eof())
{
n++;
in >> list[n];
}
}
double smallest(double data[],int n)
{
double little = data[0];
for (int j=1; j<n; j++)
if (little > data[j])
little = data[j];
return little;
}
double largest(double data[], int n)
{
double large = data[0];
for (int j=1; j<n; j++)
{
if (data[j] > large)
large = data[j];
}
return large;
}
double average(double data[], int n)
{
double sum=0.0;
for (int j=0; j<n; j++)
sum = sum + data[j];
return sum/n;
}
double variance(double data[],double ave,int n)
{
double var=0.0;
double temp=0.0;
for (int j=0; j<n; j++)
{
temp = ave - data[j];
var = pow(temp,2.0);
}
return var/n;
}
double deviation(double var)
{
double stddev=0.0;
stddev = sqrt(var);
return stddev;
}
void print_results(double small,double large,double ave,double var,double stddev,double list[],int n)
{
string filename;
ofstream out;
cout << "Please enter name of outuput file" << endl;
cin >> filename;
out.open(filename.c_str());
out << fixed << showpoint << setprecision(2);
out << "Smallest: " << small << endl;
out << "Largest: " << large << endl;
out << fixed << showpoint << setprecision(4);
out << "Average: " << ave << endl;
out << "Variance: " << var << endl;
out << "Standard Deviation: " << stddev << endl;
}
| |