[go: up one dir, main page]

0% found this document useful (0 votes)
37 views10 pages

PseudoCode Memorisation Solutions

The document defines various variables and data types, performs calculations on test scores, and uses control structures like IF/ELSE statements and loops. It declares variables like integers, reals, strings, arrays, and records. Functions and procedures are defined that take parameters and return values. Files are opened, read from and written to in order to store student name and score data.

Uploaded by

maxpir06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views10 pages

PseudoCode Memorisation Solutions

The document defines various variables and data types, performs calculations on test scores, and uses control structures like IF/ELSE statements and loops. It declares variables like integers, reals, strings, arrays, and records. Functions and procedures are defined that take parameters and return values. Files are opened, read from and written to in order to store student name and score data.

Uploaded by

maxpir06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

AS level

Declare an INTEGER variable named ‘score’, a REAL variable named ‘percentageScore’, a


CHAR variable named ‘grade’ and a STRING variable named ‘name’.
DECLARE score : INTEGER
DECLARE percentageScore : REAL
DECLARE grade : CHAR
DECLARE name : STRING

Assign the value 20 to ‘score’.


score ← 20

The maximum score is 45. Calculate the ‘percentageScore’.


percentageScore ← score * 100 / 45

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

Call the procedure using the variable ‘Alice’.


CALL giveFeedback(Alice)

What will be the output of the following pseudo code?


DECLARE text : STRING
text ← “Before being processed”
OUTPUT text
PROCEDURE changeTheText(input : STRING)
input ← “After being processed”
OUTPUT input
ENDPROCEDURE
CALL changeTheText(text)
OUTPUT text
>> Before being processed
>> After being processed
>> Before being processed

What will be the output of the following pseudo code?


DECLARE text : STRING
text ← “Before being processed”
OUTPUT text
PROCEDURE changeTheText(BYREF input : STRING)
input ← “After being processed”
OUTPUT input
ENDPROCEDURE
CALL changeTheText(text)
OUTPUT text
>> Before being processed
>> After being processed
>> After being processed

Declare an array of integers ‘arrayOfScores’.


DECLARE arrayOfScores AS ARRAY[0:5] OF INTEGER

Give the array the following values: 9, 0, 6, 8, 10, 11


arrayOfScores[0] ← 9
arrayOfScores[1] ← 0
arrayOfScores[2] ← 6
arrayOfScores[3] ← 8
arrayOfScores[4] ← 10
arrayOfScores[5] ← 11

Declare a 2D array of CHARs that is 8 by 8 named ‘chessBoard’.


DECLARE chessBoard AS ARRAY[0:7,0:7] OF CHAR

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’

Set the entire second and second-to-last row to ‘p’.


chessBoard[1,0 TO 7] ← ‘p’
chessBoard[6,0 TO 7] ← ‘p’

Set each value in rows 3 up until 6 inclusive to NULL.


chessBoard[2 TO 5,0 TO 7] ← NULL

Use a count controlled loop to create a countdown from 10 to 1.


FOR i ← 10 TO 1 STEP -1
OUTPUT i
ENDFOR

Use a post condition loop to create a countdown from 10 to 1.


DECLARE counter : INTEGER
counter ← 10
REPEAT
OUTPUT counter
counter ← counter - 1
UNTIL counter > 0

Use a pre condition loop to create a countdown from 10 to 1.


DECLARE counter : INTEGER
counter ← 10
WHILE counter > 0
OUTPUT counter
counter ← counter - 1
ENDWHILE

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

Define a pointer type which points at a CHAR.


TYPE CharPointer : ^CHAR

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

Define a set of vowels


TYPE Letters AS SET OF CHAR
DEFINE setOfVowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)

Define an Animal class with the following attributes and methods:


● Private hungry (Boolean)
● Private name (String)
● The name is passed as a function argument in the constructor, while hungry is set to
False as default
● Public speak() outputs “grrr”
● Public eat() sets the hungry to False
● Public getName() returns the name
CLASS Animal
PRIVATE hungry : BOOLEAN
PRIVATE name : STRING
PUBLIC PROCEDURE NEW(name_ : STRING)
name ← name_
hungry ← FALSE
ENDPROCEDURE

PUBLIC PROCEDURE speak()


OUTPUT “grrrr”
ENDPROCEDURE
PUBLIC PROCEDURE eat()
hungry ← FALSE
ENDPROCEDURE
ENDCLASS

Define a Dog class:


● Dog inherits Animal
● Dog overrides speak() and first outputs what the parent class speaks, followed by “bark
bark”
CLASS Dog INHERITS Animal
PUBLIC PROCEDURE speak()
SUPER.speak()
OUTPUT “bow wow”
ENDPROCEDURE
ENDCLASS

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

DECLARE studentA : Student


DECLARE studentB : Student
DECLARE filename : String
DECLARE number : INTEGER
studentA.name ← “Mark”
studentA.student_id ← 12345
filename ← “myfile.dat”
number ← 65
Open the file with the given name for random operations (like seek, get record and put record).
OPENFILE filename FOR RANDOM

Move the pointer to the position index to ‘number’.


SEEK filename, number

Put the studentA’s record at the current position of the pointer.


PUTRECORD filename, studentA

Assign the value found at position 75 in the file to studentB.


SEEK filename, 75
GETRECORD filename, studentB

Don’t forget to close the file.


CLOSEFILE filename

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

You might also like