0

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.

Martin B
  • 23,670
  • 6
  • 53
  • 72
Alter Ego
  • 39
  • 3
  • 13
  • How is your function mean() defined? Your problem may lie there also. Have you used a debugger to break into the program and see where you end up? – Martin B Feb 20 '12 at 12:35
  • here is my mean function, and no i am have never used debugger, will start now, btw i use dev c++. template 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; } – Alter Ego Feb 20 '12 at 12:41
  • I've added your definition of `mean()` to the question because it's easier to read. (You can edit your own questions, too, in case you didn't know.) – Martin B Feb 20 '12 at 12:48
  • I don't see any check for `d.size() > 0` to prevent a division by zero inside `mean()` and `stan_dev()`. Are you sure this isn't the problem? – Sjoerd Feb 20 '12 at 13:33
  • `return sum/count;` will perform rounding for `std::deque` as both `sum` and `count` will be integers. – Sjoerd Feb 20 '12 at 13:35

1 Answers1

0

Are you sure the problem is with stan_dev() and not mean()?

Using the following hacked-together implementation of mean()

template <typename T>
double mean(T begin, T end)
{
    double sum=0, count=0;
    while(begin!=end)
    {
        sum+=*begin++;
        count++;
    }

    return sum/count;
}

the program runs fine for me.

Edit: Using your version of mean(), the program runs fine for me too. What argument are you calling this on? Are you sure the hang is within stan_dev()? (Use a debugger or suitable debug outputs to find out.)

Martin B
  • 23,670
  • 6
  • 53
  • 72
  • Thanks martin, i will spend some more time on it and maybe experiment with the debugger. can you make any recommendations regarding an IDE ? i use dev c++ but it seems outdated – Alter Ego Feb 20 '12 at 12:49
  • @AlterEgo: Try here: http://stackoverflow.com/questions/89275/best-c-ide-or-editor-for-windows – Martin B Feb 20 '12 at 12:52