Calculate Standard Deviation in C++?

I have a program that will calculate max, average, etc from a file of numbers. However, I cannot get it to calculate standard deviation. Please advise.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

int main()
{
ifstream infile;
float num;
float total;
float x;
float aver;
float sumDiffSqr;
float stdDev;
vector<std::string> files = { "mynumberlist1.txt" };

for (int i = 0; i<files.size(); i++)
{
x = 0;
total = 0;
infile.open(files.at(i));
if (!infile.is_open())
{
cout << "Error opening file\n";
return 1;
}
while (!infile.eof())
{
infile >> num;
total = total + num;
x++;
}

aver = (total - num) / (x - 1);

cout << "The last number in this range is: " << num << "\n";
cout << "The sum of this range is: " << (total - num) << '\n';
cout << "The number of items in this range is: " << x - 1 << '\n';
cout << "The average of this range is: " << aver << '\n';
cout << "" << '\n';
cout << "Press enter to continue..." << '\n';

sumDiffSqr = 0;

for (i = 0; i < num; i++)
{
sumDiffSqr = sumDiffSqr + pow((num[i] - aver), 2);
}

stdDev = sqrt(sumDiffSqr / num);

infile.close();
getchar();
}

return 0;
}
I don't have mynumberlist1.txt so I can't test it, but here's some things I noticed:
- Don't return 1 because you couldn't read the file. Not being able to read a file is not an error, and your program should terminate normally with return 0.
- while (!infile.eof()) has the potential to cause problems. while ( infile >> num ) will not cause those problems, and does what you want.
- num is a float. It is not an array. You cannot call num[i] on a float.


Use code tags next time: http://www.cplusplus.com/articles/jEywvCM9/
Hi,

I rewrote the code to do this and not use eof(). I am now trying to calculate standard deviation.

The number list will be a csv file with a series of numbers; e.g.
24
63
12

The problem is that when I now calculate standard deviation, it gives a false calculation. Can you please tell me where my calculation is wrong?

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm> // std::max
#include <string>
using namespace std;

int main() {
ifstream infile("myfile11072014.csv");
string Title;
string XVariable;
getline(infile, Title);
getline(infile, XVariable);
float num;
float total = 0.0f;
unsigned int count = 0;



float sumDiffSqr;
float stdDev;

float sum = 0, max = 0, min = 0;

// While infile successfully extracted numbers from the stream
while (infile >> num) {
total += num;
++count;

sum += num;
if (num > max) max = num;
if (num < min) min = num;

}
// don't need the file anymore, close it
infile.close();

// test to see if anything was read (prevent divide by 0)
if (!count) {
std::cerr << "Couldn't read any numbers!" << std::endl;
return 1;
}

// give the average




sumDiffSqr = 0.;

for (num = 0; num < count; num++)
{
sumDiffSqr = sumDiffSqr + pow((num - (total / count)), 2);
}

stdDev = sqrt(sumDiffSqr/count);

std::cout << "Number of variables: " << count << std::endl;
std::cout << "The average is: " << total / count << std::endl;
std::cout << "The maximum is: " << max << std::endl;
std::cout << "The minimum is: " << min << std::endl;
std::cout << "The sum is: " << sum << std::endl;
std::cout << "The standard deviation is: " << stdDev << std::endl;
std::cout << "sumDiffSqr: " << sumDiffSqr << std::endl;
std::cout << "result of pow: " << pow((num - (total / count)), 2) << std::endl;
std::cout << "" << std::endl;
std::cout << "Press any key to continue... " << std::endl;



// pause the console
std::cin.sync();
std::cin.get();
getchar();
return 0;
}
There are a few things in you code that lead me to believe you have no idea what you are doing, and are just trying to copy what someone else has done. If I can see them, it's likely someone else will too. Please try to understand what you are doing.


Your calculation is wrong in the loop where you get sumDiffSqr. You appear to be trying to loop through the numbers you read in (which is what you want to do), but you are in fact looping through the variable num, which you have set to 0 and are incrementing by one each loop. You either need to save the numbers you read to an array/vector, or reopen the file and reread the numbers. I would also recommend creating a variable to store the average.
- read your numbers into an array (A)
- calculate the average (B)
- iterate over your array (A) and subtract the average (B) for each element in A. Square each result. You could store these results in another array (C)
- calculate the average of your elements in array C (D)
- take the square root of D. this is the standard deviation.
- and use code tags please :)
Last edited on
Topic archived. No new replies allowed.