Im new to c++ and i'm tryin to compute the standard deviation of a set of floating point numbers using a data file. here I started my program and skip over things i didn't really know but can someone plaese help me.
****************************************************************************

#include<iostream>
#incluse<fstream>
#include <cstdlib>
#include <cmath>

int main()
{
using namespace std;
ifstream in_stream;
ofstream out_stream;


Right in here i need help into what i put in the middle statement

}

double next,sum = 0;
double average;
double s_dev = 0;
int count = 0;
while(in_stream >> next)
{
sum = sum + next;
count++;
}
average = sum/count;

Dani AI

Generated

— a few concrete fixes and two short, safe approaches to compute standard deviation based on the replies from and .

First: fix the include typo (#incluse -> #include <fstream>), open the file and check the stream before reading, and decide whether the goal is a population SD (divide by n) or a sample SD (divide by n-1). is right: storing the values in a std::vector<double> makes the math straightforward; alternatively use an online algorithm if memory is a concern.

Two-pass (simple, accurate for small datasets):

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

int main(int argc, char* argv[]) {
    std::ifstream fin(argc>1?argv[1]:"data.txt");
    if(!fin) return 1;
    std::vector<double> vals;
    double x;
    while(fin >> x) vals.push_back(x);
    double sum = 0.0;
    for(double v : vals) sum += v;
    double mean = sum / vals.size();
    double sq = 0.0;
    for(double v : vals) { double d = v - mean; sq += d*d; }
    double pop_sd = std::sqrt(sq / vals.size());
    double sample_sd = vals.size()>1 ? std::sqrt(sq / (vals.size()-1)) : 0.0;
    std::cout << mean << ' ' << pop_sd << ' ' << sample_sd << '\n';
}

One-pass (Welford) — numerically stable and memory-light:

double mean=0.0, M2=0.0; std::size_t n=0;
for(double v : vals) { n++; double delta = v-mean; mean += delta/n; M2 += delta*(v-mean); }
double variance = (n>1) ? M2/(n-1) : 0.0; double sd = std::sqrt(variance);

Troubleshooting notes: ensure the correct filename/path, confirm the file contains whitespace-separated floats, guard against empty files, and compile with a modern standard (for example g++ -std=c++11 stdev.cpp).

Recommended Answers

All 3 Replies

Before doing anything you have to know what the data file looks like. Can you post a few lines from it? Or just attach it to your post.

The data file is a set of floating point numbers. Here they are the ones I have in my data file:
1.2
3.3
1.5
2.9
2.5
2.1

The data file is a set of floating point numbers. Here they are the ones I have in my data file:
1.2
3.3
1.5
2.9
2.5
2.1

In order to do the standard deviation, you need to store each number for later use in either an array or a vector. You can do sum and average without storing the numbers, but you can't calculate the standard deviation before knowing the average. Thus set up an array or vector and read all of the data in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.