Grade Calculator: Problem decomposition

Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )   Date: Feb 24, 2006

Concepts to be applied for the problem:

  1. Problem decomposition: breaking down a program into procedures and functions


Problem description: Your task is to take the GradeCalculator program given below, and break it up into functions and procedures. Creation of the functions and procedures should be done by breaking down the problem into its logical components based upon the different types of tasks that are being solved by the program.

The original description of the GradeCalculator program can be found at the following links:

Starting Hint: When looking at the task of creating this program there are 4 main components that can be extracted. These could help as starting points for where to break the code into functions and procedures.
  1. Reading in information from the user
    • The students grade on the test (Mark)
    • The total number of marks that a student could get on the test (TotalMarks)
    • This will also require error checking on the numbers entered by the user. The total marks should be greater then zero. The mark by the student must be greater then or equal to zero and less then the total number of marks on the test.
  2. Making some Calculations
    • Calculating the percentage the student got ( Percentage = (Mark * 100)/TotalMarks )
  3. Comparing the percentage value against the table to get a letter grade
    • This will require the use of if and if else statements
  4. Output of information to the user.

    Here is a link to the file with the original in it: GradeCalculator.p

Code


(* ==============================================================================
 *    File: GradeCalculator.p
 *  Purpose: Simple program to calculate the percentage and letter grade
 *  Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca )
 * Created on: Feb 14, 2006
  ============================================================================== *)
program GradeCalculator (input,output);

(* ========================== MAIN ============================================== *)
begin
      var Mark        : real; (* The mark the user got *)
      var TotalMarks  : real; (* Total number of possible marks *)
      var Percentage  : real; (* Used to store the calculated percentage value *)
      var LetterGrade : char; (* Used to store the letter cade for the mark *)
      var LetterSign  : char; (* Used to store the sign for the letter grade *)
      var EndLoop     : boolean; (* Used as a flag to end loops for error checking *)

      (* Read in the total marks for the test and make sure
       * it is greaterr then zero                                 *)

      EndLoop := false;
      repeat
          write('Enter the total number of marks : ');
          readln(TotalMarks);
      if (TotalMarks <= 0) then
               writeln('Error: The number must be greater then zero.')
      else
         EndLoop := true

      until EndLoop = true;

      (* Read in the mark the user got and make sure
       * that it is grwater ten or equal to zero and les then the
       *  total number of marks for the test                      *)

       EndLoop := false;
       repeat
          write('Enter your mark : ');
          readln(Mark);
          if ((Mark < 0) or (Mark > TotalMarks))  then
         writeln('Error: inncorect input')
      else
         EndLoop := true

       until EndLoop = true;

      (* Calculate the percentage value for the user *)
      Percentage := (Mark * 100.0)/TotalMarks;

      (* Find the letter grade based upon the percentage value found *)
      if (Percentage < 39.0) then
     begin
        LetterGrade := 'F'; LetterSign  := ' ';
     end
      else if (Percentage < 44.0) then
         begin
           LetterGrade := 'D'; LetterSign  := '-';
     end
      else if (Percentage < 49.0) then
     begin
        LetterGrade := 'D'; LetterSign  := ' ';
     end
      else if (Percentage < 54.0) then
     begin
        LetterGrade := 'D'; LetterSign  := '+';
     end
      else if (Percentage < 59.0) then
     begin
        LetterGrade := 'C'; LetterSign  := '-';
     end
      else if (Percentage < 64.0) then
     begin
        LetterGrade := 'C'; LetterSign  := ' ';
     end
      else if (Percentage < 69.0) then
     begin
        LetterGrade := 'C'; LetterSign  := '+';
     end
      else if (Percentage < 74.0) then
     begin
        LetterGrade := 'B'; LetterSign  := '-';
     end
      else if (Percentage < 79.0) then
     begin
        LetterGrade := 'B'; LetterSign  := ' ';
     end
      else if (Percentage < 84.0) then
     begin
        LetterGrade := 'B'; LetterSign  := '+';
     end
      else if (Percentage < 89.0) then
     begin
        LetterGrade := 'A'; LetterSign  := '-';
     end
      else
     begin
        LetterGrade := 'A'; LetterSign  := ' ';
     end;

      (* Write information to the console *)
      writeln('You got ',
               Percentage:0:2,
          '% on the test which gives you a letter grade of '
          ,LetterGrade,LetterSign);
end.
(* ============================================================================== *)