Chapter 8 - Pseudocode
Friday, May 17, 2024 5:19 PM
Psuedocode - A simple way to show an algorithm
= is an Arrow <- unless its in a condition
= in conditions. For variables you use <- ARRAY <- [50,60,70,80,90,100]
ARRAY2 <- [10,20,40,50,20,30]
OUTPUT/INPUT ARRAY3 <- [50,30,100,20,10,1000]
OUTPUT "Enter your name : " FUNCTION CalculateAverage(Array: ARRAY[1:6] OF INTEGER) RETURNS INTEGER
INPUT Name Total <- 0
FOR c <- 1 TO 6
Constant - A variable that doesn't change its value later on in the code Total <- Total + Array[c]
NEXT c
CONSTANT VariableName = value // 50+60+70+80+90+100
RETURN ROUND(Total/6, 0) // 75
Types ENDFUNCTION
ArrayOneAverage <-- CalculateAverage(ARRAY) // [50,60,70,80,90,100]
STRING - A combination of different letters and numbers - Alphanumeric - "John Smith" OUTPUT ArrayOneAverage // 75
CHAR - A singular letter - 'A'
INTEGER - A whole number - 53
REAL - A number going into decimal points - 6784.23
BOOLEAN - TRUE or FALSE - TRUE
DECLARATION SYNTAX
DECLARE VariableNames(seperated by commas(,)) : Type
DECLARE Num1, Num2 : INTEGER
DECLARE Name : STRING
CONDITIONAL STATEMENTS
IF…THEN...ELSE…ENDIF
IF Condition
THEN Condition result is 1
ELSE Condition result is 0
ENDIF
0/FALSE 1/TRUE 0 /FALSE
(Num1 > Num2) OR NOT (Num2 > Num3) OR (Num4 > Num3)
CASE OF…ENDCASE
A singular variable is compared to other values
CASE OF VariableName
Value1: Statement
Value2: Statement
Value3: Statement
ENDCASE
OUTPUT "Enter num between 1-3"
INPUT Num
CASE OF Num
1: OUTPUT "Hello"
2: OUTPUT "Bye"
3: OUTPUT "You're stupid"
OTHERWISE: OUTPUT "You're too stupid"
ENDCASE
Loops
Going over a code over and over and over and over in iterations
WHILE…DO…ENDWHILE - Pre condition loop
REPEAT…UNTIL - Post condition loop
FOR…TO…NEXT - Count controlled loop
FOR VariableAssigning TO Limit
ACTION
Next VariableName
FOR c <- 0 TO 50
ACTION
NEXT c
OUTPUT "Enter a number between 1 and 50"
INPUT Num
WHILE Num < 1 OR Num > 50 DO
OUTPUT "You stupid little guy enter a number between 1 and 50"
INPUT Num
New Section 1 Page 1
INPUT Num
ENDWHILE
REPEAT
OUTPUT "Enter a number between 1 and 50"
INPUT Num
UNTIL Num < 1 OR Num > 50
1 - 50
// 49
Num < 1 OR Num > 50 // 0
Num >= 1 AND Num <=50 // 1
Totalling, Counting, Max Min Avg.
A Teacher is inputting the records of 50 students. Total all records and
output them. Use a FOR loop
Total = 0
for count in range(1,50):
record = int(input('Input student record for student : '))
Total = Total + record
print(Total)
DECLARE Total, Record : INTEGER
FOR Count <- 1 TO 50
INPUT Record
Total <- Total + Record
NEXT Count
OUTPUT Total
Counting
A teacher inputs 50 students marks. Count how many of them passed. Passed = above
50. Use a FOR loop
Passed = 0
for count in range(1,50):
marks = int(input('Enter student marks : '))
if(marks > 50):
Passed = Passed + 1
DECLARE Passed, Marks : INTEGER
Passed <- 0
FOR Count <- 1 TO 50
INPUT Marks
IF Marks > 50
THEN Passed <- Passed + 1
ENDIF
NEXT Count
OUTPUT Passed
Minimum, Maximum, Average
A teacher inputs 5 students marks. The maximum achievable is 100. Minimum is 0.
Calculate highest, lowest, and average.
Highest = 0
Lowest = 100
Total = 0
for count in range(0,5):
mark = int(input('Enter mark : '))
if(mark > Highest):
Highest = mark
if(mark < Lowest):
Lowest = mark
Total = Total + mark
avg = Total/5
print(Highest, Lowest, avg)
'''
Test Data = 40, 70, 10, 5, 90
Lowest - 40 , Highest - 40
Lowest - 40 , Highest - 70
Lowest - 10 , Highest - 70
New Section 1 Page 2
Lowest - 10 , Highest - 70
Lowest - 5 , Highest - 70
Lowest - 5 , Highest - 90
Output - 90, 5, 43.0
'''
DECLARE Highest, Lowest, Average, Mark, Total : INTEGER
Total <- 0
Highest <- 0
Lowest <- 100
FOR student <- 1 TO 5
INPUT Mark
IF Mark > Highest
THEN Highest <- Mark
ENDIF
IF Mark < Lowest
THEN Lowest <- Mark
ENDIF
Total <- Total + Mark
NEXT student
Average <- Total/5
OUTPUT Highest
OUTPUT Lowest
OUTPUT Average
Functions & Procedures
You're the teacher at a school. Your task is to add all student's marks and figure out the grade.
Function is used to run a module and return a value as the answer
FUNCTION FunctionName(Parameters(seperated by commas)) RETURNS ReturnType
FunctionActions
RETURN Val
ENDFUNCTION
FUNCTION AddMarks(MathMarks: INTEGER, EnglishMarks: INTEGER, PhysicsMarks: INTEGER,
ComputerMarks: INTEGER, ChemistryMarks: INTEGER) RETURNS INTEGER
// 10, 10, 10, 9, 10
TotalMarks <-- 0
TotalMarks <-- MathMarks + TotalMarks // 10
TotalMarks <-- EnglishMarks + TotalMarks // 20
TotalMarks <-- ChemistryMarks + TotalMarks // 30
TotalMarks <-- ComputerMarks + TotalMarks // 39
TotalMarks <-- PhysicsMarks+ TotalMarks // 49
RETURN TotalMarks // 49
ENDFUNCTION
TotalMarksOfStudent <-- AddMarks(10, 10, 10, 9, 10)
Procedures
DO NOT return a value
PROCEDURE ProcedureName(Parameters)
ProcedureWork
ENDPROCEDURE
SendStars(5)
*****
PROCEDURE SendStars(Stars: INTEGER)
FOR c <-- 1 TO Stars
OUTPUT "*"
NEXT
ENDPROCEDURE
Arrays
Just a collection of different variables of the same data type.
An array is identified by []
Inside of an array each value is seperated by a comma (,)
[10, 20, 30, 40, 50] - 1D Array
DECLARE array: ARRAY[1:5] OF INTEGER
array <-- [1,2,3,4,5]
1 2 3 4 5
DECLARE arrayName: ARRAY[NoOfRows:NoOfColumns] OF DataType
DECLARE Array: ARRAY[1:5, 1:6] OF INTEGER
Array <- [[10,20,30,40,50, 6],[60,70,80,90,100, 6],[10,40,20,23,21, 6],[9,4,2,2,1, 6],[21,23,21,32,33, 6]]
New Section 1 Page 3
Array <- [[10,20,30,40,50, 6],[60,70,80,90,100, 6],[10,40,20,23,21, 6],[9,4,2,2,1, 6],[21,23,21,32,33, 6]]
An array AccDetails[] is a 2D Array with 50 clients information. And each client has their balance,
withdrawal balance & debt balance inside.
DECLARE AccDetails: ARRAY[1:50, 1:3] OF INTEGER
DECLARE CoolArray: ARRAY[1:3, 1:5] OF INTEGER
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Routine Syntax Routine Description Routine Example with Output
MOD(ValueUsedToDivide, Returns the Remainder of the division MOD(4,2)
ValueDividedBy) 4 MOD 2 in PSEUDOCODE
Popular Case : Even number divided by 2
will return 0
DIV(ValueUsedToDivide, Returns the Quotiant of the division DIV(10,3)
ValueDividedBy) 10 DIV 3 in PSEUDOCODE
ROUND(Number, DecimalPoints) Rounds the number to the amount of d.p specified ROUND(6.7382, 2) -> 6.74
RANDOM() Returns a random number B/W 1 and 0 Inclusive RANDOM() -> 1 or 0
LEFT(string, Returns the string from the left to the amount of LEFT("Ateeb Sohail", 5) -> "Ateeb"
amountOfCharactersToLeft) character specified
RIGHT(string, Returns the string from the right to the amount of RIGHT("Ateeb Sohail", 6) -> "Sohail"
amountOfCharactersToRight) character specified
MID(String, Position, Returns the string from the position it started at MID("Ateeb Sohail", 3, 5) -> "eeb S"
AmountOfCharacters)
LENGTH(String) Returns the length of the string LENGTH("Ateeb Sohail") -> 12
UCASE(String) Returns string in upper case UCASE("Ateeb Sohail") -> ATEEB SOHAIL
LCASE(String) Returns string in lower case LCASE("Ateeb Sohail") -> ateeb sohail
Array <- ['Ateeb', 'Rick', 'Brittany', 'Megatron', 'Sam']
New Section 1 Page 4