Solved

# Average and standard deviation

Posted on 2005-04-27
1,648 Views
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.
0
Question by:cuong5985

LVL 8

Expert Comment

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

LVL 22

Expert Comment

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

LVL 11

Expert Comment

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

LVL 9

Accepted Solution

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

Author Comment

#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

Expert Comment

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

## Featured Post

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.