Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing an IEnumerable of Numeric Values as a parameter to method
    primarykey
    data
    text
    <p>I'm playing around with a very simple program to take an array of doubles and return the standard deviation. This part worked but I wanted to make the code more reusable. I would like to make it so the method can accept a parameter of any type that could be considered numeric and return the standard deviation instead of hardcoding a double type (like I initially did in this program). How does one go about this and what is the proper term for it?</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { double[] avg = { 3.4, 55.6, 10.0, 4.5, 2, 2 }; double x = avg.Average(); //first round of testing Console.WriteLine("The average of the first array is below "); Console.WriteLine(x); Console.WriteLine("below should be the standard deviation!"); Console.WriteLine(CalculateStandardDeviation(avg)); Console.ReadLine(); int[] intAvg = { 4, 3, 5, 6, 2 }; double secondAvg = intAvg.Average(); Console.WriteLine("The average of the second array is below "); Console.WriteLine(secondAvg); //this is where the error is happening //CalculateStandardDeviation(secondAvg); } //this is where I tried to make the query more reusable public static double CalculateStandardDeviation(IEnumerable&lt;double&gt; values) { double avg = values.Average(); double sum = 0; foreach (double d in values) { sum += Math.Pow((d - avg), 2); } return Math.Pow(sum / (values.Count() - 1),.5); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload