Standard Deviation?!

closed account (3796URfi)
I need help with calculating standard deviation can someone please help me

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main ()
{

    
    cout << "\nThis program will produce statistics (Mean, Standard Deviation, "
    "Maximum and Minimum values of the list) for a list of integer values."
    "The user will provide the names of input and output files.\n" ; // << endl;
    
    cout <<"\nEnter name and location for input file:   " ;
    string file_input ;
    getline(cin, file_input);
    ifstream fin( file_input.c_str() ) ;
    
    if(fin.fail())
    {
        cout << "Bad file name or location.\n" ;
        return 0 ;
    }
    
    cout <<"Enter name and location for output file:   ";
    string file_output ;
    getline(cin, file_output);
    ofstream fout( file_output.c_str() );
    
    cout << "\nReading values for first time. . .\n" ;
    fout << "\nReading values for first time. . .\n" ;
    
    int num, variance=0 ;
    int count = 0 ;
    int total = 0 ;
    int Min = 100;
    int Max  = 0;
    
 
    while( fin >> num )     {
        cout << num << ' ' ;
        fout << num << ' ' ;
        if( ++count%10 == 0 )
        {
            cout << '\n' ;
            fout << '\n' ;
        }
     
        total += num ;
        if( num > Max ) Max = num ;
        if( num < Min ) Min = num ;
    }
    
    double sumsq=0;
    if( count > 0 )
    {
        const double avg = double(total) / count ;
        sumsq+= num * num;
        
        cout << "\n\nNumber of values read:  " << count;
        cout << "\nMean of the values:  " << avg << '\n' ;
        cout << "\nStandard deviation using method 1:   ";
        cout << "Greatest value:  " << Max << '\n' ;
        cout << "Least value:  " << Min << '\n' ;
        
        fout << "\n\nNumber of values read:  " << count;
        fout << "\n\nMean of the values :  " << avg << '\n' ;
        fout << "\nStandard deviation using method 1:   ";
        fout << "Greatest value :  " << Max << '\n' ;
        fout << "Least value :  " << Min << '\n' ;
    }
    
    return 0;
}
closed account (D80DSL3A)
You will need to find one other number as you read the values from the file. You need the sum of the squares of the numbers:
totalSq = num1*num1 + num2*num2 + ...

So declare a double totalSq = 0.0;
and inside your while loop:
totalSq += num*num;

Then double stdDev = sqrt( (totalSq - total*total/count)/count );

You will need to #include<cmath> for the sqrt function.

EDIT: I see you tried it at line 60, but that's too late. It belongs in the while loop.
Last edited on
closed account (3796URfi)
I tried everything and calculated in the while loop but my calculations aren't accurate! please help I've been trying for hours
closed account (3796URfi)
Can you tell me what I'm doing wrong I'm very confused with the looping. thanks

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

int main ()
{
    
    
    cout << "\nThis program will produce statistics (Mean, Standard Deviation, "
    "Maximum and Minimum values of the list) for a list of integer values."
    "The user will provide the names of input and output files.\n" ; // << endl;
    
    cout <<"\nEnter name and location for input file:   " ;
    string file_input ;
    getline(cin, file_input);
    ifstream fin( file_input.c_str() ) ;
    
    if(fin.fail())
    {
        cout << "Bad file name or location.\n" ;
        return 0 ;
    }
    
    cout <<"Enter name and location for output file:   ";
    string file_output ;
    getline(cin, file_output);
    ofstream fout( file_output.c_str() );
    
    cout << "\nReading values for first time. . .\n" ;
    fout << "\nReading values for first time. . .\n" ;
    
    int num;
    int count = 0 ;
    int total = 0 ;
    int Min = 100;
    int Max  = 0;
    double totalSq=0.0, stdDev;
    
    
    fin >> num;
    while( !fin.eof() )
    {
        cout << num << ' ' ;
        fout << num << ' ' ;
        if( ++count%10 == 0 )
            
        {
            cout << '\n' ;
            fout << '\n' ;
        }
        
        {
            total += num ;
            if( num > Max ) Max = num ;
            if( num < Min ) Min = num ;
            fin >>num;
        }
        {
            
            totalSq+= num*num;
            stdDev = sqrt((totalSq - total*total/count)/count);
        }
        
        const double avg = double(total) / count ;
        
        cout << "\n\nNumber of values read:  " << count;
        cout << "\nMean of the values:  " << avg << '\n' ;
        cout << "\nStandard deviation using method 1:   " << " " << stdDev;
        cout << "\nGreatest value:  " << Max << '\n' ;
        cout << "Least value:  " << Min << '\n' ;
        
        fout << "\n\nNumber of values read:  " << count;
        fout << "\n\nMean of the values :  " << avg << '\n' ;
        fout << "\nStandard deviation using method 1:   ";
        fout << "Greatest value :  " << Max << '\n' ;
        fout << "Least value :  " << Min << '\n' ;
    }
    
    return 0;
}
closed account (D80DSL3A)
You put everything in the while loop?
You just needed to add one line inside the while loop.
Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double totalSq = 0.0;

while( fin >> num )     {
        cout << num << ' ' ;
        fout << num << ' ' ;
        if( ++count%10 == 0 )
        {
            cout << '\n' ;
            fout << '\n' ;
        }
     
        total += num ;
        totalSq+= num*num;// new code
        if( num > Max ) Max = num ;
        if( num < Min ) Min = num ;
    }

You then use the formula for stdDev in the lower code section
1
2
3
4
5
6
7
8
if( count > 0 )
    {
        const double avg = double(total) / count ;
        sumsq+= num * num;
        
        cout << "\n\nNumber of values read:  " << count;
        cout << "\nMean of the values:  " << avg << '\n' ;
        cout << "\nStandard deviation using method 1:   " <<  sqrt((totalSq - total*total/count)/count);

Perhaps change total to type double, instead of int. Integer division may be affecting the result in sqrt((totalSq - total*total/count)/count)

You switched back to this?
1
2
 fin >> num;
    while( !fin.eof() )


You're going backwards.
Last edited on
closed account (3796URfi)
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

int main ()
{


cout << "\nThis program will produce statistics (Mean, Standard Deviation, "
"Maximum and Minimum values of the list) for a list of integer values."
"The user will provide the names of input and output files.\n" ; // << endl;

cout <<"\nEnter name and location for input file: " ;
string file_input ;
getline(cin, file_input);
ifstream fin( file_input.c_str() ) ;

if(fin.fail())
{
cout << "Bad file name or location.\n" ;
return 0 ;
}

cout <<"Enter name and location for output file: ";
string file_output ;
getline(cin, file_output);
ofstream fout( file_output.c_str() );

cout << "\nReading values for first time. . .\n" ;
fout << "\nReading values for first time. . .\n" ;

int num;
int count = 0 ;
double total = 0 ;
int Min = 100;
int Max = 0;
double totalSq=0.0;


while(fin >>num )
{
cout << num << ' ' ;
fout << num << ' ' ;
if( ++count%10 == 0 )

{
cout << '\n' ;
fout << '\n' ;
}

{
total += num ;
if( num > Max ) Max = num ;
if( num < Min ) Min = num ;
totalSq+= num*num;
}


}
if (count>0)
{
const double avg = double (total) / count ;
double stdDev = sqrt((totalSq - total*total/count)/count);

cout << "\n\nNumber of values read: " << count;
cout << "\nMean of the values: " << avg << '\n' ;
cout << "\nStandard deviation using method 1: " << " " << totalSq << " " << stdDev;
cout << "\nGreatest value: " << Max << '\n' ;
cout << "Least value: " << Min << '\n' ;

fout << "\n\nNumber of values read: " << count;
fout << "\n\nMean of the values : " << avg << '\n' ;
fout << "\nStandard deviation using method 1: ";
fout << "Greatest value : " << Max << '\n' ;
fout << "Least value : " << Min << '\n' ;
}

return 0;
}

you're right it worked perfectly! thank you for your help I really appreciate it. I was trying to use while(!fin.eof()) because I'm required to use it than (fin>>num) but it obviously causes problems
closed account (D80DSL3A)
You're welcome. BTW the formula I gave works, but it's more complicated than necessary. Try:
double stdDev = sqrt( sumSq/count - avg*avg );
Last edited on
Topic archived. No new replies allowed.