Click here to Skip to main content
13,145,568 members (45,233 online)
Rate this:
Please Sign up or sign in to vote.
Guys. Im a c# noob but I really need a professional's help. I am using visual studio 2005 for a project so I dont have math.linq I need to calculate the standard deviation of a generic list of objects. The list contains just a list of float numbers, nothing too complicated. However I have never done this before so i need someone to show me that has calculated standard deviation of a generic list before. Here is my code that I tried to start on:

//this is my list of objects inside. The valve data just contains a bunch of floats.
public class ValveDataResults
    private List<valvedata> m_ValveResults;
    public void AddValveData(ValveData valve)
    //this is the function where the standard deviation needs to be calculated:
    public float LatchTimeStdev()
        //below this is a working example of how to get an average or mean
        //of same list and same "LatchTime" that needs to be calculated here as well. 
    //function to calculate average of latch time, can be copied for above st dev.
    public float LatchTimeMean()
        float returnValue = 0;
        foreach (ValveData value in m_ValveResults)
            returnValue += value.LatchTime; 
        returnValue = (returnValue / m_ValveResults.Count) * 0.02f;
        return returnValue;

the "Latch Time" is a float object of the "ValveData" object which is inserted into the m_ValveResults list.

thats it. any help would much be appreciated. Thanks

[Modified: if you add <pre lang="cs"> before the code and </pre> after the code, it will format it for you!]
Posted 30-Jun-10 4:25am
Updated 30-Jun-10 4:32am
Johnny J.168.3K

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

A simple google search of c# and standard deviation gave this:

private double getStandardDeviation(List<double> doubleList)
   double average = doubleList.Average();
   double sumOfDerivation = 0;
   foreach (double value in doubleList)
      sumOfDerivation += (value) * (value);
   double sumOfDerivationAverage = sumOfDerivation / doubleList.Count;
   return Math.Sqrt(sumOfDerivationAverage - (average*average));
Tom Hangler 30-Jun-10 10:44am
many things wrong with this. First as I mentioned, I have VS 2005 which wont support math.Linq therefore no Average. Also I wont be passing anything through parameters. And mine is float not double. Thanks for da help but I need a little more. Possible someone who did this sort of thing before
William Winner 30-Jun-10 10:56am
Regardless of what you're using, there is a math library available for pretty much every language. If you're using c#, then you definitely have System.Math. If you didn't, that would would surprise me. Math.Linq isn't necessary.

Secondly, a double is just a larger float. If you can't change the word double in that code to float, then you're not going to get far in programming.

Thirdly, if you can't figure out how to write your own Average, then again, not going to get far.
Tom Hangler 30-Jun-10 11:01am
thank you. it helped a lot.
Tom Hangler 30-Jun-10 11:01am
i figured it out. Sorry for confusion. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 30 Jun 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100