[ create a new paste ] login | about

Link: http://codepad.org/teMq0fsZ    [ raw code | output | fork ]

C++, pasted on Feb 20:
#include <deque>
#include <algorithm>
#include <iostream>

using namespace std;

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<(int)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;
}

int main()
{
    deque<double> d;
    d.push_back(1);
    d.push_back(2);
    d.push_back(3);

    cout << endl << stan_dev(d);

    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11

 Deviation at for age # 1 is -1
 Deviation squared for age # 1 is 1
 Summing up so far .. 1
 Deviation at for age # 2 is 0
 Deviation squared for age # 2 is 0
 Summing up so far .. 1
 Deviation at for age # 3 is 1
 Deviation squared for age # 3 is 1
 Summing up so far .. 2
0.816497


Create a new paste based on this one


Comments: