12,755,986 members (38,044 online)
Rate this:
See more: , +
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;

{
}

//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;
}</valvedata>```

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 5:25am
Updated 30-Jun-10 5:32am
Johnny J.162.3K
v3

Rate this:

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

Top Experts
Last 24hrsThis month
 Graeme_Grant 446 Maciej Los 315 Peter Leow 173 ppolymorphe 160 OriginalGriff 158
 OriginalGriff 4,089 Peter Leow 3,139 ppolymorphe 2,463 Karthik Bangalore 2,375 Graeme_Grant 1,723