# Average and standard deviation

Topic write a program that takes its input from a file of numbers of type doulbe.  The program outputs to the screen the average and standard deviation of the numbers in the file. The file contains nothing but numbers of type doulb separated by blanks and or line breaks.  The standard deviation of a list of number 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.....
a is the average of the number.

My program.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
using namespace std;

void stdv (double n1, double n2, double n3, double n4, double avg);

int main()
{
ifstream in_stream;
ofstream out_stream;

in_stream.open("infile_pp4.txt");
if (in_stream.fail( ))
{
cout <<"Input file opening failed.\n";
getch();
exit(1);
}
out_stream.open("outfile_pp4.txt");
if (out_stream.fail( ))
{
cout <<"Output file opening failed.\n";
getch();
exit(1);
}

double n1, n2, n3, n4;
in_stream >>n1>>n2>>n3;
out_stream <<"The average of the numbers are "<<((n1+n2+n3+n4)/4)<<endl;

in_stream.close( );
out_stream.close( );

system("PAUSE");
return 0;
}

This can produce the average but i dont know how to start wih deviation, can someone give me the correct way to do it.
###### Who is Participating?

Commented:
/(n-1) is correct. if anyone teaches /n fire him/her and the recruiter.
0

Commented:
here it is...
#include <iostream>
#include <fstream>
#include <cstdarg>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;

double avg(int n, va_list v)
{
double sum = 0;

for ( int x = 0; x < n; x++ )
sum += va_arg ( v, double );
va_end ( v );

return sum / n;
}
double stdv (int n, ...)
{

va_list arguments;
va_start(arguments, n);
double mean =  avg(n, arguments);
double sum = 0;
for ( int x = 0; x < n; x++ ){
int val = va_arg ( arguments, double );
sum += ((val - mean)*(val - mean));
}

va_end ( arguments );

return sqrt(sum/(n));
}

int main()
{
ifstream in_stream;
ofstream out_stream;

in_stream.open("infile_pp4.txt");
if (in_stream.fail( ))
{
cout <<"Input file opening failed.\n";
getch();
exit(1);
}
out_stream.open("outfile_pp4.txt");
if (out_stream.fail( ))
{
cout <<"Output file opening failed.\n";
getch();
exit(1);
}

double n1, n2, n3, n4;
in_stream >>n1>>n2>>n3>>n4;

out_stream <<stdv(4, n1, n2, n3, n4)<<endl;
in_stream.close( );
out_stream.close( );

system("PAUSE");
return 0;
}

_novi_
0

Commented:
This looks like homework to me, but I'll give a clue anyway.

To calculate standard deviation, you only need to use 3 variables:
n=number of values
sum = sum of values
sumsq = sum of squares of values

It's possible to take the definition of standard deviation, sqrt(sum((x-mean)^2)/(n-1)), and convert it into terms of those 3 variables above through algebraic manipulation.
0

Commented:
There are two formulae for std deviation.  One is the one quoted by NovaDenizen, the other is sqrt(sum((x-mean)^2)/n).    Most calculators do it NovaDenizen's way, some schools teach the other method.  I've got no idea which one is correct as I have been taught both by different schools.
0

Author Commented:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <string>
using namespace std;

int main( )
{
ifstream fin;
ofstream fout;

string question ;
cout << "Type Your Question : " ;
cin >> question ;

fin.open("infile_pp13.txt");
if (fin.fail( ))
{
cout <<"Input file opening failed.\n";
getch();
exit(1);
}

fout.open("outfile_pp13.txt");
if (fout.fail( ))
{
cout <<"Output file opening failed.\n";
getch();
exit(1);
}
double n1, n2, n3, n4;
cin>>n1>>n2>>n3>>n4;

out_stream <<stdv(4, n1, n2, n3, n4)<<endl;

fin.close( );
fout.close( );

system("PAUSE");
return 0;
}

double avg(int n, va_list v)
{
double sum = 0;

for ( int x = 0; x < n; x++ )
sum += va_arg ( v, double );
va_end ( v );

return sum / n;
}
double stdv (int n, ...)
{

va_list arguments;
va_start(arguments, n);
double mean =  avg(n, arguments);
double sum = 0;
for ( int x = 0; x < n; x++ ){
int val = va_arg ( arguments, double );
sum += ((val - mean)*(val - mean));
}

va_end ( arguments );

return sqrt(sum/(n));
}

this is not work, can so help
0

Commented:
This code is what I need but with a little twist. I need the program to open and close the input file three separate. One when the mean is calculated, then when the variance is calculated and then when the std deviation is calculated. Then instead of just outputting to a file, I would like to display all three on the screen? Can anyone help? I have included the snippet. The mean is not right.
``````#include <iostream>
#include <fstream>
#include <cstdarg>
#include <cmath>
#include <cstdlib>
#include <conio.h>
using namespace std;

double avg(int n, va_list v)
{
double sum = 0;

for ( int x = 0; x <n; x++ )
sum += va_arg ( v, double );
va_end ( v );

return sum / n;
}
double stdv (int n, ...)
{

va_list a;
va_start(a, n);
double mean =  avg(n, a);
double sum = 0;
for ( int x = 0; x < n; x++ )
{
int val = va_arg ( a, double );
sum += ((val - mean)*(val - mean));
}

va_end ( a );

return sqrt(sum/(n));
}

int main()
{
ifstream in_stream;
ofstream out_stream;

in_stream.open("assign7_in.txt");
if (in_stream.fail( ))
{
cout <<"Input file opening failed.\n";
getch();
exit(1);
}
out_stream.open("assign7_out.txt");
if (out_stream.fail( ))
{
cout <<"Output file opening failed.\n";
getch();
exit(1);
}

double n1, n2, n3;
in_stream >>n1>>n2>>n3;

cout <<" The standard deviation is " <<stdv(3, n1, n2, n3)<<endl;
cout <<" The Average is " <<avg<<endl;

in_stream.close( );
out_stream.close( );

system("PAUSE");
return 0;
}
``````
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.