// $Id: moments.csharp,v 1.0 2002/11/27 13:19:00 dada Exp $ // http://dada.perl.it/shootout/ // // Transliterated from the Java implementation. // // contributed by Isaac Gouy using System; using System.Collections; namespace LanguageShootout { class Moments { [STAThread] static void Main(string[] args) { String line; ArrayList numbers = new ArrayList(); double num, sum = 0.0; double mean = 0.0; double average_deviation = 0.0; double standard_deviation = 0.0; double variance = 0.0; double skew = 0.0; double kurtosis = 0.0; double median = 0.0; double deviation = 0.0; int i, n, mid = 0; while ( (line = Console.ReadLine()) != null ) { num = Double.Parse(line); sum += num; numbers.Add(num); } n = numbers.Count; mean = sum / n; for (i=0; i<n; i++) { deviation = (double)numbers[i] - mean; average_deviation += Math.Abs(deviation); variance += Math.Pow(deviation,2); skew += Math.Pow(deviation,3); kurtosis += Math.Pow(deviation,4); } average_deviation /= n; variance /= (n - 1); standard_deviation = Math.Sqrt(variance); if (variance != 0.0) { skew /= (n * variance * standard_deviation); kurtosis = kurtosis/(n * variance * variance) - 3.0; } numbers.Sort(); mid = n / 2; median = (n % 2 != 0) ? (double)numbers[mid] : ((double)numbers[mid] + (double)numbers[mid-1]) / 2; Console.WriteLine("n: {0:d}", n); Console.WriteLine("median: {0:f6}", median); Console.WriteLine("mean: {0:f6}", mean); Console.WriteLine("average_deviation: {0:f6}", average_deviation); Console.WriteLine("standard_deviation: {0:f6}", standard_deviation); Console.WriteLine("variance: {0:f6}", variance); Console.WriteLine("skew: {0:f6}", skew); Console.WriteLine("kurtosis: {0:f6}", kurtosis); } } }