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
|
#include <iostream>
#include <fstream>
#include <cctype>
#include <cmath>
#include <cstdlib>
using namespace std;
double calc_average(const int value[], const int COUNT);
double calc_std(const int value[], int COUNT, double average);
const int array_size = 100;
int main (){
ifstream input;
double average, std;
int value[array_size], COUNT = 0;
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
input.open("ints-1-col.dat");
if (input.fail())
{
cout<<"Input file opening failed"<<endl;
exit(1);
}
while (input>>value[COUNT])
COUNT++;
average = calc_average(value, COUNT);
std = calc_std(value, COUNT, average);
cout<<COUNT<< " values for average = "<<average<< " and for standard deviation = " <<std<<endl;
input.close();
return (0);
}
double calc_average(const int value[], const int COUNT)
{
int i;
int sum = 0;
double average;
for (i=0; i<COUNT; i++)
sum = sum + value [i];
average = static_cast<double>(sum)/static_cast<double>(COUNT);
return (average);
}
double calc_std(const int value[], int COUNT, double average)
{
int e;
double final_std, sum_x_squares, add_sum_x_squares;
for (e = 0; e<COUNT; e++){
sum_x_squares = (value[e] - average)*(value[e] - average);
add_sum_x_squares = sum_x_squares + add_sum_x_squares;
}
final_std = sqrt(add_sum_x_squares/static_cast<double>(COUNT));
return (final_std);
}
|