PseudoCode Memorisation Solutions
PseudoCode Memorisation Solutions
Declare a constant ‘thresholdA’ and a constant ‘thresholdB’. Set them to 60 and 40 respectively.
CONSTANT thresholdA = 60
CONSTANT thresholdB = 40
Anyone scoring 60% or more gets an ‘A’. Anyone scoring between 40% and 60% get’s a ‘B’.
Everyone else gets a ‘C’. Determine the grade using the constants from the previous question.
IF (percentageScore >= thresholdA) THEN
grade ← ‘A’
ELSE IF (percentageScore >= thresholdB) THEN
grade ← ‘B’
ELSE
grade ← ‘C’
ENDIF
Get input from the user. The user should enter his or her name. Store the value in the variable
‘name’.
OUTPUT “Please enter your name:”
INPUT name
Output the message: “Hello [name]. You have scored [percentage_score] which is a [grade]”
DECLARE message : STRING
message ← “Hello “ & name & “. You have scored “ & percentageScore &
“which is a “ & grade
OUTPUT message
If the percentage score is greater than 85% then output “Excellent”. If the percentage score is
between 70% and 85% then output “Good”. If the percentage score is between 55% and 70%
then output “Fine”. In all other cases output the message “Keep trying and you will reach your
potential!”. Use a case structure to write pseudocode for this condition.
CASE OF grade:
85 +: OUTPUT “Excellent”
70 TO 85: OUTPUT “Good”
55 TO 70: OUTPUT “Fine”
OTHERWISE: “Keep trying and you will reach your potential!”
ENDCASE
Declare a boolean variable ‘pass’. A student passes if he/she is absent 5 or less times AND if
he/she has 6 or more passes. Using two new integers ‘noOfAbsences’ and ‘noOfPasses’ write
an IF clause that sets the appropriate value to the pass variable.
DECLARE pass : BOOLEAN
IF (noOfAbsences <= 5 AND noOfPasses >= 6) THEN
pass ← TRUE
ELSE
pass ← FALSE
ENDIF
Kevin scores 40 out of 50, 60 out of 100, and 61 out of 90. Convert each score to a percentage
and calculate the average. Then round down the average to the nearest integer.
DECLARE averageRoundedDown : INTEGER
averageRoundedDown ← INT((40/50 + 60/100 + 61/90)*100/3)
Jim has 22 sweets. He tries to share the sweets equally amongst himself and his 4 friends. How
many sweets does each friend get? How many sweets are left over? (Use DIV and MOD)
DECLARE noSweets : INTEGER
DECLARE noSweetsLeftOver : INTEGER
noSweets ← 22 DIV 5
noSweetsLeftOver ← 22 MOD 5
Declare a boolean variable ‘cheating’ and set the value to FALSE. If Kevin was not cheating
then output the message “Good job Kevin, you passed”
DECLARE cheating : BOOLEAN
cheating ← FALSE
IF NOT cheating THEN
OUTPUT “Good job Kevin, you passed”
ENDIF
IF cheating = FALSE THEN
OUTPUT “Good job Kevin, you passed”
ENDIF
IF cheating <> TRUE THEN
OUTPUT “Good job Kevin, you passed”
ENDIF
Amy missed one exam. She can retake it if she has a doctor’s note or if she has a note from her
parents. Declare two boolean variables and use IF and OR to determine if Amy can retake the
exam or not.
DECLARE doctorNote : BOOLEAN
DECLARE parentNote : BOOLEAN
doctorNote ← FALSE
parentNote ← TRUE
IF parentNote OR doctorNote THEN
OUTPUT “You can retake the exam”
ELSE
OUTPUT “You cannot retake the exam”
ENDIF
Jake does not know the answer to a multiple choice question. Write pseudocode to help him
pick an answer at random: a, b, c or d.
DECLARE randomNumber : INTEGER
DECLARE letter : CHAR
randomNumber ← RAND(4)
CASE OF randomNumber:
0: letter ← ‘a’
1: letter ← ‘b’
2: letter ← ‘c’
3: letter ← ‘d’
ENDCASE
Create a new variable type named StudentRecord. The record should contain his/her lastName
(STRING), dateOfBirth (DATE) and three score values englishScore, mathScore and
historyScore which are REAL.
TYPE StudentRecord
DECLARE lastName : STRING
DECLARE dateOfBirth : DATE
DECLARE englishScore : REAL
DECLARE mathScore : REAL
DECLARE historyScore : REAL
ENDTYPE
Create a variable ‘Alice’ of type StudentRecord. Set the attributes according to the following:
● lastName “Smith”
● dateOfBirth 25/March/2010
● englishScore 68
● mathScore 72
● historyScore 71
DECLARE Alice : StudentRecord
Alice.lastName ← “Smith”
Alice.dateOfBirth ← 25/03/2010
Alice.englishScore ← 68
Alice.mathScore ← 72
Alice.historyScore ← 71
Write a function that takes a StudentRecord type as a parameter and returns the average of the
three scores.
FUNCTION average(param1 : StudentRecord) RETURNS REAL
DECLARE ave : REAL
DECLARE score1 : REAL
DECLARE score2 : REAL
DECLARE score3 : REAL
score1 ← param1.englishScore
score2 ← param1.mathScore
score3 ← param1.historyScore
ave ← (score1 + score2 + score3)/3
RETURN ave
ENDFUNCTION
Write a procedure which takes a StudentRecord type as a parameter, calculates the average
score and outputs “Well done!” if the average is greater than 70.
PROCEDURE giveFeedback(parameter : StudentRecord)
DECLARE a : REAL
a ← average(parameter)
IF a > 70 THEN
OUTPUT “Well done!”
ENDIF
ENDPROCEDURE
Sets the top row and the bottom row to ‘r’, ‘n’, ‘b’, ‘q’, ‘k’, ‘b’, ‘n’, ‘r’.
chessBoard[0,0] ← ‘r’
chessBoard[0,1] ← ‘n’
chessBoard[0,2] ← ‘b’
chessBoard[0,3] ← ‘q’
chessBoard[0,4] ← ‘k’
chessBoard[0,5] ← ‘b’
chessBoard[0,6] ← ‘n’
chessBoard[0,7] ← ‘r’
chessBoard[7,0] ← ‘r’
chessBoard[7,1] ← ‘n’
chessBoard[7,2] ← ‘b’
chessBoard[7,3] ← ‘q’
chessBoard[7,4] ← ‘k’
chessBoard[7,5] ← ‘b’
chessBoard[7,6] ← ‘n’
chessBoard[7,7] ← ‘r’
A file named “AllStudentNames.txt” contains a list of student names. The first line contains a
student name, the next line contains his or her student id. The next two lines contain the next
student’s name and id, etc… Open the file for reading.
OPENFILE “AllStudentNames.txt” FOR READ
Read all the names and student id numbers. Put each name into an array of strings
‘studentNames’. Put each student id number into an array of strings named ‘studentid’. (FYI
there will never be more than 1000 students)
DECLARE studentNames AS ARRAY[0:999] OF STRING
DECLARE studentId AS ARRAY[0:999] OF INTEGER
DECLARE counter : INTEGER
counter ← 0
WHILE NOT EOF(“AllStudentNames.txt”)
READFILE “AllStudentNames.txt”, studentNames[counter]
READFILE “AllStudentNames.txt”, studentId[counter]
counter ← counter + 1
ENDWHILE
Close the file.
CLOSEFILE “AllStudentNames.txt”
Open a new file for writing. The filename is the first student’s name with “.txt” added to the end.
Write the corresponding student id number to the file. Then close the file again.
DECLARE filename : STRING
filename ← studentNames[0] & “.txt”
OPENFILE filename FOR WRITE
WRITEFILE filename, “Student ID: “ & studentId[0]
CLOSEFILE filename
The following two arrays show the first student’s list of subjects and scores.
DECLARE subjects AS ARRAY[0:5] OF STRING
DECLARE scores AS ARRAY[0:5] OF INTEGER
subjects[0] ← “Geography”
subjects[1] ← “French”
subjects[2] ← “Computer Science”
subjects[3] ← “English”
subjects[4] ← “Maths”
subjects[5] ← “Economics”
scores[0] ← 9
scores[1] ← 6
scores[2] ← 7
scores[3] ← 4
scores[4] ← 7
scores[5] ← 6
Add each subject and score to the file line by line, and then close the file.
//write all the scores to file
DECLARE lineToAdd : STRING
OPENFILE filename FOR APPEND
FOR i ← 0 TO 5 STEP 1
lineToAdd ← subjects[i] & “: ” & scores[i]
WRITEFILE filename, lineToAdd
ENDFOR
CLOSEFILE filename
A2 level
User Defined Data Types
Define an enumerated type State which can be Solid, Liquid or Gas.
TYPE State : (Solid, Liquid, Gas)
Declare a new variable with identifier ‘substance1’ and of type State. Set the value to Liquid.
Then increment the state.
DECLARE substance1 : State
substance1 ← Liquid
substance1 ← substance1 + 1
Referencing: Declare a CHAR variable and a CharPointer variable. Assign the address of the
CHAR variable to the CharPointer variable.
DECLARE a : CHAR
DECLARE charPointer : CharPointer
a ← ‘z’
charPointer ← ^a
Dereferencing: Declare another CHAR variable and assign the value that can be found at the
address charPointer.
DECLARE b : CHAR
b ← charPointer^
Define a record type named book with the attributes title (string), author (string) and no_pages
(integer).
TYPE Book:
DECLARE title : STRING
DECLARE author : STRING
DECLARE no_pages : INTEGER
ENDTYPE
Declare a new variable with identifier newBook of type Book. Set the title, author and no_pages
to “Shakespeare”, “Macbeth” and 105 respectively.
DECLARE newBook : Book
newBook.title ← “Macbeth”
newBook.author ← “Shakespeare”
newBook.no_pages ← 105
Create an instance of Dog with the name “Fido”. Ask Fido to speak.
DECLARE myDog : Dog
myDog ← NEW Dog(“Fido”)
myDog.speak()
Binary Files
Use the following code to answer the questions below.
TYPE Student
DECLARE name : STRING
DECLARE student_id : INTEGER
ENDTYPE
Exception Handling
Open a file named “test.txt” for writing. If an error occurs output “Something went wrong.”
TRY
OPENFILE “test.txt” FOR WRITE
EXCEPT
OUTPUT “Could not write to test.txt. Do you have write access?”
ENDTRY