Building a stats program for C++ practice, I extract ages of subjects from a file and store them in deque<int> ages
. However, when I try to obtain a standard deviation, I encounter an error:
template <class U> typename U::value_type mean(U begin,U end){
typedef typename U::value_type res_type;
res_type sum=res_type();
size_t count=0;
for(U pos=begin;pos!=end;++pos){
sum+=*pos;
++count;
}
return sum/count;
}
template <typename T>
double stan_dev(deque<T>&d){
double dev_sum=0,dev,dev_sqr,st_dev;
double mn=mean(d.begin(),d.end());
for(int it=0;it<d.size();++it){
T val=d[it];
dev= val-mn;
dev_sqr = dev*dev;
dev_sum+=dev_sqr;
cout<<"\n Deviation at for age # "<<d[it]<<" is "<<dev;
cout<<"\n Deviation squared for age # " <<d[it]<<" is "<<dev_sqr;
cout<<"\n Summing up so far .. "<<dev_sum;
}
st_dev=sqrt((dev_sum/d.size()));
return st_dev;
}
Just giving the relevant portion here, cout << stan_dev(ages)
, and the program hangs. Please help.