0% found this document useful (0 votes)
0 views

Computer Science psuedocode

The document outlines essential pseudocode concepts including operators, program structure, loops, variables, data types, and user-defined data types. It provides examples of declaring variables, calculating scores, using conditional statements, and creating functions and procedures. Additionally, it illustrates the use of arrays and records in pseudocode with practical examples.

Uploaded by

peacendwar32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Computer Science psuedocode

The document outlines essential pseudocode concepts including operators, program structure, loops, variables, data types, and user-defined data types. It provides examples of declaring variables, calculating scores, using conditional statements, and creating functions and procedures. Additionally, it illustrates the use of arrays and records in pseudocode with practical examples.

Uploaded by

peacendwar32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

All the pseudocode we need to learn:

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

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” 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)

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: 0, 6, 8, 10, 11

arrayOfScore [0] <-9


asrrayOfScores [1]<-0
asrrayOfScores [2]<-6
arrayOfScores [3]<-8
arrayOfScores[4] <-10
arrayOfScores[4] <-11
Declare a 2D array of CHARs that is & by named chess Board

DECLARE chessboard AS ARRAY [0:8] OF CHAR


Set the top row and the bottom row to r,n, b, q, k,b,c,d
chesaboard[0,0]<- ‘r’
chesaboard[0,1]<- ‘n’
chesaboard[0,2]<- ‘b’
chesaboard[0,3]<- ‘q’
chesaboard[0,4]<- ‘k’
chesaboard[0,5]<- ‘b’
chesaboard[0,6]<- ‘c’
chesaboard[0,7]<- ‘d’
chesaboard[7,0]<- ‘r’
chesaboard[7,1]<- ‘n’
chesaboard[7,2]<- ‘b’
chesaboard[7,3]<- ‘q’
chesaboard[7,4]<- ‘k’
chesaboard[7,5]<- ‘b’
chesaboard[7,6]<- ‘c’
chesaboard[7,7]<- ‘d’
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 row 3 up until 6 inclusive to NULL
chessboard[3 TO 6,0 TO 7] <- NULL

You might also like