Professional Documents
Culture Documents
HND PRG W4 Algorithms With Pseudocode
HND PRG W4 Algorithms With Pseudocode
HND PRG W4 Algorithms With Pseudocode
1
Lesson Learning Outcome
Pass Merit Distinction
2
Data Declaration
▪ Data Type: This indicates the type of value it can store, such as
integer, string, real etc.
▪ Data Scope: This determines where the data item can be used.
For example, is it Global or Local?
▪ Data Usage: This determines how the data can be used. Can it
be changed (Variables) or is it fixed (Constant)
3
Receiving Input
4
Sending Output
5
Assigning Values to Variable
6
Performing Computer Arithmetic
7
Procedures and Functions
8
Parameters
9
Techniques of Parameter Passing
Call by Value : Module receives a
copy of the original data item as a
parameter. Therefore changes made
within the module doesn't alter
original data.
10
Procedure Example
Example1
11
Function Example
Example1
12
its
13
INPUT “Enter Limit “,Last
Count = 1
Sum = 0
WHILE Count <= Last
Sum = Sum + Count
Count = Count + 1
END WHILE
DISPLAY “Total = “,Sum
14
FOR Num = 1 TO 10 STEP 1
Rem = Num MOD 2
IF Rem = 0 THEN
Type = “Even”
ELSE
Type = “Odd”
END IF
DISPLAY Num, “: “, Type, “ Number”
END FOR
15
INPUT “Enter 3 Numbers”, Left, Mid, Right PROCEDURE Swap(First, Second)
IF Mid < Left THEN
Hold = First
Call Swap(Left, Mid)
END IF First = Second
IF Right < Left THEN Second = Hold
Call Swap(Left, Right)
END PROCEDURE
END IF
IF Right < Mid THEN
Call Swap(Mid, Right)
END IF
DISPLAY Left, Mid, Right
16
INPUT “Start & Stop”, N1, N2 FUNCTION FindSum (First, Last)
WHILE N1 <> N2 Tot = 0
IF N1 > N2 THEN
FOR Num = First TO Last STEP 1
DISPLAY “Invalid Data”
ELSE Tot = Tot + Num
DISPLAY “Tot = “,FindSum(N1, N2) END FOR
END IF RETRUN Tot
INPUT “Start & Stop”, N1, N2 END FUNCTION
END WHILE
17
INPUT “Enter Sentence ”, Str
INITIALIZE all Counters
FOR X = 1 TO LENTGH of Str
Ch = Str[X] // Take current character into Ch
CASE Ch OF
‘a’, ’A’ : aCount = aCount + 1
‘e’, ’E’ : eCount = eCount + 1
‘i’, ’I’ : iCount = iCount + 1
‘o’, ’O’ : oCount = oCount + 1
‘u’, ’U’ : uCount = uCount + 1
END CASE
END FOR
DISPLAY all Counters
18
Lesson Summary
▪ Data Declaration
▪ Receiving Input / Sending Output
▪ Initializing Variables / Computations
▪ Functions / Procedures
▪ Parameters and their roles
▪ Call by Value / Call by Reference
▪ Exercises
19