1 // Student: Pat Moss www.patmoss.com/cis162ad/ 2 // Instructor: Dave Hammer http://members.cox.net/dwhammer/ 3 4 // CIS162AD -- C Sharp (C#) -- Section 5832 5 // Professor: Dave Hammer dwhammer@cox.net 6 // http://members.cox.net/dwhammer/ 7 // Student: Patrick Moss patmoss@patmoss.com 8 // http://www.patmoss.com/cis162ad/ 9 10 // Worksheet #4 11 // III. Programming Exercise 12 // Write a program to calculate the average of all scores entered 13 // between 0 and 100. Use a sentinel-controlled loop variable to 14 // terminate the loop. After values are entered and average 15 // calculated, test the average to determine whether an A, B, C, 16 // D, or F should be recorded. The scoring rubric is as follows: 17 // A: 90 to 100, B: 80 to 89, C: 70 to 79, D: 60 to 69, F: less 18 // than 60. Turn in your source code for this program. 19 20 // program name: calcaverage.cs rev. 01 02-25-2006 by pat moss 21 // textbook: Visual C# .NET Programming, Barbara Doyle, 0619159979 22 23 using System; 24 25 namespace calcaverage 26 { 27 class calcaverage 28 { 29 static void Main() 30 { 31 bool moreToDo = true; 32 double examScore = 0.0; 33 int examCount = 0; 34 double examSum = 0.0; 35 double examAverage = 0.0; 36 int examAverage1 = 0; 37 char examGrade = ' '; 38 39 Console.WriteLine("Worksheet4 -- calcaverage.cs"); 40 Console.WriteLine("Student: Pat Moss"); 41 Console.WriteLine("Instructor: Dave Hammer"); 42 43 displayInstructions(); 44 45 do 46 { 47 Console.Write("Enter the next Exam score: "); 48 examScore = Convert.ToDouble(Console.ReadLine()); 49 50 // test for a negative sentinel exit value present 51 if (examScore < 0.0) 52 moreToDo = false; // yes -- exit from the loop now 53 else if (examScore > 100.0) // no -- test for invalid Exam score present 54 Console.WriteLine("**Error: Invalid Exam Score: {0}", examScore); 55 else // value is ok, so process the current Exam score 56 { 57 examCount++; 58 examSum += examScore; 59 } 60 } while (moreToDo == true); 61 62 // calculate the average of all Exam scores 63 // and round to two decimal places 64 examAverage = Math.Round((examSum / (double) examCount), 2); 65 66 // determine a letter grade for the Exam score average 67 // where A = 90-100, B = 80-89, C = 70-79, D = 60-69, 68 // and F = 0-59 69 examAverage1 = (int) (examAverage / 10.0); 70 switch (examAverage1) 71 { 72 case 9: 73 case 10: 74 examGrade = 'A'; 75 break; 76 case 8: 77 examGrade = 'B'; 78 break; 79 case 7: 80 examGrade = 'C'; 81 break; 82 case 6: 83 examGrade = 'D'; 84 break; 85 default: 86 examGrade = 'F'; 87 break; 88 } 89 90 Console.WriteLine(" "); 91 Console.WriteLine("Number of exams: {0}", examCount); 92 Console.WriteLine("Sum of exams: {0:F2}", examSum); 93 Console.WriteLine("Average of exams: {0:F2}", examAverage); 94 Console.WriteLine("Letter grade: {0}", examGrade); 95 96 Console.WriteLine("\nAll done..."); 97 } 98 99 private static void displayInstructions() 100 { 101 Console.WriteLine("Exam Average and Grade Calculator\n"); 102 Console.WriteLine("Enter each Exam Score on a separate line."); 103 Console.WriteLine("Note: Scores are rounded to two decimal places."); 104 Console.WriteLine("Enter a negative value to calculate results...\n"); 105 } 106 } 107 } 108