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
|
//Write a program that takes its input from a file of numbers of type double.
//The program outputs to the screen the average and the standard deviation of the numbers in the file.
//The file contains nothing but numbers of type double separated by blanks and/or line breaks.
//The standard deviation of a list of numbers n1, n2, n3, and so forth is defined as the square root of
//the average of the following numbers:
//(n1 – a)2, (n2 – a)2, (n3 – a)2, and so forth
//The number a is the average of the numbers n1, n2, n3, and so forth.
//Hint: Write your program so that it first reads the entire file and computes the average of all the numbers,
// and then closes the file, then reopens the file and computes the standard deviation
#include <iostream>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;
int stdDeviation( double n[], int avg) // computes the standard deviation
{
int i;
double sd = 0;
cout.setf(ios::fixed);
sd=sqrt(pow((n[i]-avg),2));
return (sd);
}
int main()
{
ifstream fin;
ofstream fout;
//cout.setf(ios::showpoint);
//create input file , read data , compute the sum, count the numbers , calculate average , calculate stdDev
fin.open("inputFile.dat");
if ( fin.fail())
{
cout<<" Error in Opening Input File .......'\n"<<endl;
}
else
{
cout<<" Opening Input File '\n";
exit(1);
}
//}
fout.open("outputFile",ios::app);
if ( fout.fail())
{
cout<<" Error in creating Output file.....'\n";
}
else
{
cout<<" Creating output File '\n";
}
double n1,n2,n3,n4,n5,inputNo[6],sum = 0;
int i,average = 0, count = 0 , stdDev = 0 ;
cout.setf(ios::fixed);
setw(6);
do
{
//inputData= fin.get("inputFile.dat");
for ( i=0; i<= count; i++)
{
sum += inputNo[i];
count++;
average = sum / count;
}
}
while (!=fin.eof())
{
fin>>n1>>n2>>n3>>n4>>n5;
stdDev = stdDeviation( inputNo, average);
{
cout<<" End of File... Terminating ...!"<<endl;
}
fout<<" The output is as follows ; '\n";
fout<<n1<<n2<<n3<<n4<<n5<<endl;
fout<<" The total sum is "<<sum<< endl;
fout<<" while the average is "<<average<<endl;
fout<<" for the total of "<<count<<" numbers computed "<<endl;
fout<<" Standard deviation is "<< stdDev<<endl;
fin.close( );
fout.close( );
return 0;
}
|