Grade book using a rectangular array to store grades. - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Grade book using a rectangular array to store grades.

Demo Code

using System;/*from ww w .  j a v  a  2  s . c o  m*/
class MyCourse
{
   private int[,] grades;
   public string CourseName { get; }
   public MyCourse(string name, int[,] gradesArray)
   {
      CourseName = name; // set CourseName to name
      grades = gradesArray; // initialize grades array
   }
   public void DisplayMessage()
   {
      Console.WriteLine($"Welcome to the grade book for\n{CourseName}!\n");
   }
   public void ProcessGrades()
   {
      OutputGrades();
      Console.WriteLine($"\nLowest grade in the grade book is {GetMinimum()}" + $"\nHighest grade in the grade book is {GetMaximum()}\n");
      OutputBarChart();
   }
   public int GetMinimum()
   {
      var lowGrade = grades[0, 0];
      foreach (var grade in grades)
      {
         if (grade < lowGrade)
         {
            lowGrade = grade;
         }
      }
      return lowGrade; // return lowest grade
   }
   public int GetMaximum()
   {
      var highGrade = grades[0, 0];
      foreach (var grade in grades)
      {
         if (grade > highGrade)
         {
            highGrade = grade;
         }
      }
      return highGrade; // return highest grade
   }
   public double GetAverage(int student)
   {
      var gradeCount = grades.GetLength(1);
      var total = 0.0; // initialize total
      for (var exam = 0; exam < gradeCount; ++exam)
      {
         total += grades[student, exam];
      }
      return total / gradeCount;
   }
   public void OutputBarChart()
   {
      Console.WriteLine("Overall grade distribution:");
      var frequency = new int[11];
      foreach (var grade in grades)
      {
         ++frequency[grade / 10];
      }
      for (var count = 0; count < frequency.Length; ++count)
      {
         if (count == 10)
         {
            Console.Write("  100: ");
         }
         else
         {
            Console.Write($"{count * 10:D2}-{count * 10 + 9:D2}: ");
         }
         // display bar of asterisks
         for (var stars = 0; stars < frequency[count]; ++stars)
         {
            Console.Write("*");
         }
         Console.WriteLine(); // start a new line of output
      }
   }
   public void OutputGrades()
   {
      Console.Write("            "); // align column heads
      for (var test = 0; test < grades.GetLength(1); ++test)
      {
         Console.Write($"Test {test + 1}  ");
      }
      for (var student = 0; student < grades.GetLength(0); ++student)
      {
         Console.Write($"Student {student + 1,2}");
         for (var grade = 0; grade < grades.GetLength(1); ++grade)
         {
            Console.Write($"{grades[student, grade],8}");
         }
         Console.WriteLine($"{GetAverage(student),9:F}");
      }
   }
}
class MyCourseTest
{
   static void Main()
   {
      // rectangular array of student grades
      int[,] gradesArray = {{81, 96, 70},
      {61, 87, 90},
      {91, 100, 90},
      {110, 81, 82},
      {81, 65, 81},
      {71, 87, 61},
      {81, 75, 81},
      {91, 94, 110},
      {71, 72, 81},
      {81, 93, 71}};
      MyCourse myMyCourse = new MyCourse("Programming", gradesArray);
      myMyCourse.DisplayMessage();
      myMyCourse.ProcessGrades();
   }
}

Result


Related Tutorials