Computer Science psuedocode
Computer Science psuedocode
Operators:
Conditions:
Logical operators:
NOT
AND
OR
Mathematical operators
DIV
MOD
INT
RAND
Program structure:
IF true/false THEN ELIF ELSE... ENDIF
CASE structure
Loops:
REPEAT
FOR
WHILE
INPUT
OUTPUT
PROCEDURE
FUNCTION
BYREF
Variables:
DECLARE
CONSTANT
Data types:
BOOLEAN
CHAR
DATE
INTEGER
REAL
STRING
Records: TYPE
Files:
OPEN
FOR READ
FOR WRITE
FOR APPEND
READFILE
WRITEFILE
EOF
CLOSEFILE
Arrays
ARRAY
2D ARRAY
Comments
Understanding pseudocode string functions
A2 Level
User defined data types:
ENUM
POINTER
RECORD
SET
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” 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 percentageScore:
percentageScore>=85 :OUTPUT "Excellent"
percentageScore>=70: OUTPUT "Good"
percentageScore>=55: 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 (noof Absences < 5 AND noof Passes >= 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 average: REAL
DECLARE averageRounded: INTEGER
average (40/50 +60/100 + 61/90) *100/3
averageRoundedDown INT(average)
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 parent Note: BOOLEAN
doctorNote <-TRUE
parentNote <-TRUE
IF parent Note 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 Student Record
DECLARE lastName: STRING
DECLARE dateOfBirth: DATE
DECLARE englishScore: REAL
DECLARE mathScore: REAL
DECLARE historyScore: REAL
ENDTYPE
Create a variable 'Alice' of type Student Record. Set the attributes according to the
following:
lastName "Smith"
dateOfBirth 25/March/2010
englishScore 68
mathScore 72
historyScore 71
DECLARE Alice: Student Record
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: Student Record) RETURNS REAL
DECLARE ave: REAL
DECLARE score1: REAL
DECLARE score2: REAL
DECLARE score3: REAL
scorel <- paraml.englishScore
score2<- paraml.mathScore
score3<- paraml.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 give Feedback (parameter: Student Record)
DECLARE a:REAL
a<- average (parameter)
IF a > 70 THEN
OUTPUT "Well done!”
ENDIF
ENDPROCEDURE
Call the procedure using the variable 'Alice'.
CALL give Feedback(Alice)