2

I'm a c# noob but I really need a professional's help. I am using visual studio 2005 for a project so I don't 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)
    {
        m_ValveResults.Add(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.

That's it. Any help would much be appreciated. Thanks

Greg
  • 23,155
  • 11
  • 57
  • 79
Tom Hangler
  • 783
  • 1
  • 5
  • 7
  • 5
    Welcome to Stackoverflow. You have a great question, but for future reference, "PLZ HELP!!??@11" is not a good question title. – Rex M Jun 30 '10 at 14:25
  • 3
    Why would you accept [this answer](http://stackoverflow.com/questions/3141692/c-standard-deviation-of-generic-list/3141731#3141731) and then ask the same question again? – John Rasch Jun 30 '10 at 14:32
  • 1
    @John - I noticed that in this question he specifies that he is using VS 2005. So he wouldn't be able to use Linq. – Greg Jun 30 '10 at 14:36
  • @Greg: Then he should un-accept that answer and modify that question to disallow linq. –  Jun 30 '10 at 14:45

1 Answers1

1

Try

public float LatchTimeStdev()
{
    float mean = LatchTimeMean();
    float returnValue = 0;
    foreach (ValveData value in m_ValveResults)
    {
        returnValue += Math.Pow(value.LatchTime - mean, 2); 
    }
    return Math.Sqrt(returnValue / m_ValveResults.Count-1));
}

Its just the same as the answer given in LINQ, but without LINQ :)

fbstj
  • 1,684
  • 1
  • 16
  • 22