Пример #1
0
 static void PrintStatistics(ObservationStatistics stats)
 {
     Console.WriteLine($"Observation with greatest height:\tHeight = {stats.GreatestHeightObservation.Height}, Weight = {stats.GreatestHeightObservation.Weight}");
     Console.WriteLine($"Observation with greatest weight:\tHeight = {stats.GreatestWeightObservation.Height}, Weight = {stats.GreatestWeightObservation.Weight}");
     Console.WriteLine($"Observation with least height:\tHeight = {stats.LeastHeightObservation.Height}, Weight = {stats.LeastHeightObservation.Weight}");
     Console.WriteLine($"Observation with least weight:\tHeight = {stats.LeastWeightObservation.Height}, Weight = {stats.LeastWeightObservation.Weight}");
     Console.WriteLine($"Mean Height:\t{stats.MeanHeight}");
     Console.WriteLine($"Mean Weight:\t{stats.MeanWeight}");
     Console.WriteLine($"Median Height:\t{stats.MedianHeight}");
     Console.WriteLine($"Median Weight:\t{stats.MedianWeight}");
     Console.WriteLine($"Height Standard Deviation:\t{stats.HeightStandardDeviation}");
     Console.WriteLine($"Weight Standard Deviation:\t{stats.WeightStandardDeviation}");
 }
Пример #2
0
        // Only method by which to obtain an instance of this class
        public static ObservationStatistics CalculateStatistics(List <Observation> obs)
        {
            if (obs == null || obs.Count == 0)
            {
                throw new ArgumentException("Invalid input.");
            }
            int   m = (obs.Count / 2) - 1;
            float h = 0.0f, w = 0.0f;
            ObservationStatistics result = new ObservationStatistics();

            result.GreatestHeightObservation = result.GetObservationWithComparisonPredicate(obs
                                                                                            , (lhs, rhs) => lhs.Height.CompareTo(rhs.Height));
            result.GreatestWeightObservation = result.GetObservationWithComparisonPredicate(obs
                                                                                            , (lhs, rhs) => lhs.Weight.CompareTo(rhs.Weight));
            result.LeastHeightObservation = result.GetObservationWithComparisonPredicate(obs
                                                                                         , (lhs, rhs) => - 1 * lhs.Height.CompareTo(rhs.Height));
            result.LeastWeightObservation = result.GetObservationWithComparisonPredicate(obs
                                                                                         , (lhs, rhs) => - 1 * lhs.Weight.CompareTo(rhs.Weight));

            obs.ForEach((ob) => {
                result.MeanHeight += ob.Height;
                result.MeanWeight += ob.Weight;
            });
            result.MeanHeight /= (float)obs.Count;
            result.MeanWeight /= (float)obs.Count;

            if ((obs.Count & 1) == 0)
            {
                result.MedianHeight = (obs[m].Height + obs[m + 1].Height) / 2.0f;
                result.MedianWeight = (obs[m].Weight + obs[m + 1].Weight) / 2.0f;
            }
            else
            {
                result.MedianHeight = obs[m].Height;
                result.MedianWeight = obs[m].Weight;
            }

            obs.ForEach((ob) => {
                h = h + (float)Math.Pow((ob.Height - result.MeanHeight), 2.0);
                w = w + (float)Math.Pow((ob.Weight - result.MeanWeight), 2.0);
            });
            h = h / (float)obs.Count;
            w = w / (float)obs.Count;
            result.HeightStandardDeviation = (float)Math.Sqrt(h);
            result.WeightStandardDeviation = (float)Math.Sqrt(w);
            return(result);
        }
Пример #3
0
        static void Main(string[] args)
        {
            try
            {
                if (args == null || args.Length <= 1 || !File.Exists(args[1]))
                {
                    throw new Exception("Invalid command line argument");
                }

                List <Observation> obs = null;
                using (StreamReader sr = new StreamReader(args[1]))
                {
                    obs = ObservationReader.ReadObservations(sr);
                }
                ObservationStatistics stats = ObservationStatistics.CalculateStatistics(obs);
                PrintStatistics(stats);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Unexpected exception occured: {ex.Message}.");
            }
        }