public GradesStatistics ComputeStatistics() { GradesStatistics stats = new GradesStatistics(); float sum = 0f; foreach(float grade in grades) { stats.HighestGrade = Math.Max(grade, stats.HighestGrade); stats.LowestGrade = Math.Min(grade, stats.LowestGrade); sum += grade; } stats.AverageGrade = sum / grades.Count; return stats; }
static void Main(string[] args) { GradeBook book = new GradeBook(); book.AddGrade(91); book.AddGrade(89.5f); //Ending float types with 'f' to indicate float book.AddGrade(62.4f); GradesStatistics stats = book.ComputeStatistics(); //Console.Writeline code snippet: 'cw' (TAB x2) Console.WriteLine("Average: " + stats.AverageGrade); Console.WriteLine("Highest: " + stats.HighestGrade); Console.WriteLine("Lowest: " + stats.LowestGrade); //Ctrl + F5 allows for a pause on console before closing when //Program is done (Press any key to continue ... ) }
//If return type is another class that has less accessibility than current class, //Then this function will call an error public GradesStatistics ComputeStatistics() { GradesStatistics stats = new GradesStatistics(); float sum = 0; //All grades in gradebook\ //Foreach: will execute once for every item in a collection foreach (float grade in grades) { //if(grade > stats.HighestGrade) //{ // stats.HighestGrade = grade; //} stats.HighestGrade = Math.Max(grade, stats.HighestGrade); //Sets to highest of two variables passed stats.LowestGrade = Math.Min(grade, stats.LowestGrade); //Sets to lowest of two variables sum += grade; } stats.AverageGrade = sum / grades.Count; return(stats); }