Grade Calculator Solution |
|
Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca ) Date: Feb 14, 2006 | |
When looking at the task of creating this program there are 4 main components that can be extracted:
Here is a link to the file with the solution in it: GradeCalculator.p | |
Code |
Extra Info |
(* ============================================================================== * 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. (* ============================================================================== *) |
Variable declaration: Mark, TotalMarks, Percentage are all declared as real (floating point) to keep the type consistent for the calculation of the percentaged. Because the division of the mark and total mark could result in decimal places it is a good idea to declare then as real. Read in information: For error checking each case for reading in information from the user is encased in a repeat - until loop. This will force the user to enter the proper values before the program continues. In the case of TotalMarks we have to make sure the number is greater then zero. For logical sense the total marks could not be negative. Also we want to make sure the TotalMarks is not zero to prevent a runtime error when we calculate the percentage. Basic Calculation: find the percentage the user got on the test Find a letter grade: Find the letter grade based upon the results from the calculation made above. This is done by comparing the value using an if else ladder. Output information: To keep everything at one point all the information is outputted to the user at the end of the program instead of at incremental points. This allows for quick and easy changes in one location, if required. |