HND PRG W4 Algorithms With Pseudocode

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

UNIT1 - Programming

Week [04] –Algorithms with Pseudocode

1
Lesson Learning Outcome
Pass Merit Distinction

LO1 Define basic algorithms to carry out an operation and


outline the process of programming an application

P1 Provide a definition of M1 Determine the step taken D1 Examine the


what an algorithm is and from writing code to implementation of an
outline the process in execution. algorithm in a suitable
building an application. language. Evaluate the
relationship between the
written algorithm and the
code variant.

2
Data Declaration

When writing pseudocode, we usually omit the data declaration part to


keep the simplicity. There are 3 important aspects data considers:

▪ 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

▪ There are some keywords that can be Some Examples:


used in pseudocode for receiving input
INPUT marks1, marks2
▪ INPUT : Receive data values from
READ Student_Record
keyboard
GET Emp_Name
▪ GET : Receive data values from
keyboard

▪ READ : Receive data values from a file

4
Sending Output

▪ When the computer is ready with the Some Examples:


results it has to supply this information to
the display unit or any other output device DISPLAY “Total = “,sum
or a file.
OUTPUT sum,average
▪ DISPLAY : Sends output to the screen
▪ PUT : Sends output to the screen WRITE Emp_Record

▪ OUPUT : Sends output to the screen PRINT name,age


▪ PRINT : Sends the output to the printer
▪ WRITE : Sends out to a file

5
Assigning Values to Variable

Variables are memory locations identified Some Examples:


by user defined names. It is important
that we store initial values in these INITIALIZE total TO 0
variables.
SET Count to 1
▪ SET : Store an initial value in a
Count = 1
variable
▪ INITIALIZE : Store an initial value in a
variable
▪ “=” : This also could be used

6
Performing Computer Arithmetic

The results of such computations also must be Some Examples:


stored in variables. Following keywords are
used for this purpose. CALCULATE Sum = x + y
COMPUTE : Perform any arithmetic and store COMPUTE Avg as Sum / 2
the result in a variable
CALCULATE : Perform any arithmetic and ADD marks into Total
store the result in a variable
Sum = x + y
ADD, MULTIPLY, DIVIDE, SUBTRACT : These
can also be used to perform a specific SUBTRACT loan from Salary
arithmetic operation

7
Procedures and Functions

Procedure : A unit of a program that carries


out some well-defined operation on data
specified by parameters. It can be called
from anywhere in the program and different
parameters can be provided for each call.

Function : The difference between a Procedure and the function is


that the function will return a single value after called, whereas a
procedure does a particular task but not necessarily returns a value.

8
Parameters

Parameters : These are Data items sent from


a calling module to a function or procedure that
is called. It can serve 1 of the following roles.
▪ Input parameter - Supply input data of the
module

▪ Output parameter - Sends the output of the


module

▪ Input/output parameter - Supply input data


and sends out the changed data

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.

Call by Reference : Module receives


a reference to the original data item
itself. Therefore any change made
within the module will alter original
data item as well.

10
Procedure Example

Example1

11
Function Example

Example1

12
its

INPUT “Enter Height “, height


INPUT “Enter Width “, width
COMPUTE area = 1 / 2 * height * width
DISPLAY “Area = “,area

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

You might also like