Algorithm Writing Rules
Algorithm Text three blocks: 2. Header
- algorithm name - brief description about the program
2. Declaration - definition of all variables, constant values, etc
3. Description - the main part
(1) Header Algorithm Circle_Area
{ calculating the area of a circle. Input will be the radius of the circle }
(2) Declaration Declaration { constants } const phi = 3.14 { type } type Point : record {cartesian coord.} < x: integer, y: integer >
(3) Description Description read (c,d) if c < d then e a+b else e a-b endif write e
Working example Algorithm Calculating_Mean { calculating the mean of an integer group } Declaration X : integer N : integer K : integer SUM : integer AVE : real
{ input } { the number of the input } { pointer } { the sum } {the mean }
Description read (N) k1 SUM 0
{ read the number of the data} {initial value for looping} {initial value of the SUM}
while k <= N do {looping until k = N } read (x) SUM SUM + x {accumulation} kk+1 endwhile AVE SUM / N Write (AVE)
{averaging} {final output}
Pseudo code descriptive using
similar language programming no single standard •example: variable X is given the value of 0. X 0 variable X is given the value of previous X plus 2 X X+2
Pseudo code (2) Reading
input data x Read (x) Printing output data x Write (x)
Pseudo code – condition Determine
positive or negative
if X => 0 then Write ("positive") else Write ("negative") Endif
Pseudo code – loop total
accumulation of the first 100 numbers, starting value x = 1 x 1 While (x <=100) Do Accum Accum + x x x+1 Endwhile
Pseudocode For (x 1 to 100) Do Accum Accum + X Endfor X 1 Repeat Accum Accum +1 X X+1 Until X > 100
Flowchart Graphical
representation Easy to understand Avoid mis-interpretation Think systematically
Flowchart – the symbols 1
3
Start / Stop 2. Initial value 3. Read / print 4. Process 1.
2
4
Flowchart – the symbols (2) 5
6
7
5. Condition checking 6. Connection @ one page 7. Dokumen 8 Predefined process
8
Flowchart - example Calculate
shape Input: L, W
the area of a rectangular START Read L, W Calculate A=LxW Print A
FINISH
Types, names and values Boolean Logics Type : boolean Value : 0 (false) or 1(true) operations logical operations not, and, or, nor, xor
a
not a
True
False
False
True
(1) Boolean logics A True True False
B True False True
False
False
A and B A or B True True False True False True False
X true, y false, Z true (x and y) or z ?? x and (y or z) ?? not (x and z) ??
False
A xor B False True True False
(2) Integer Type
name: integer Value: Theoretical
: (+) infinite to (-) infinite But we can make a limitation saving memory allocation Example: @TP7 type
Value
Byte
0 … 255
Shortint
-128 … 127
Word
0 … 65535
Integer
-32768 … 32767 ( 16-bit = 2 byte)
longint
-2^32 … 2^32-1 (32-bit)
(16-bit)
Integer - operations Arithmetics
(+); (-); (*); div; mod
10 mod 3 (result: 1) 10 div 3 (result: 3) 10 & 3 is operands, mod is operator
•
Comparisons < ; > ; = ; >= ; <= ; <> (24 div 3) <> 8 (false)
(3) Real 18.0
; 0.065 ; 16.3E-2
• Type name: real • Value: – we make a limitation saving memory allocation Example: @TP7 type
Value
Real
2.9 E-39 to 1.7 E38
Single
1.5 E-45 to 3.4 E38
Double
5 E-324 to 1.7 E308
Extended
3.4 E-4932 to 1.1 E4932
6 byte
Real - operations Arithmetics
(+); (-); (*); /;
10 / 4 (result: 2.5 ) 10 & 3 is operands, / is operator
•
Comparisons < ; > ; >= ; <= ; <>
(3) Character Alphanumerics 1,2,#,%,&,A,a
Type
name: char Constant using ‘ ' 8 integer '8' character
Character - operations Comparison
=, <>, <=, >= 't' = 't' true 't' = 'T' false
(4) Record Consist
of one or more fields Each field may store data with any basic type (boolean, integer, etc) field 1
field 2
…
field n
type Point : record < x : real, y : real > type Point : record < x , y : real >
Example ID
Name
Course Code
Mark
Type StudMark : record < ID
: integer
Name: string
{student ID} {student name}
CourseCode : string { Course Code} Mark: char >
{A/B/C/D/E/F}
NAME As
an identifier to ‘something’ Must be unique ‘Something’: 1. Variable Declaration X, mark : real K: integer C: char
Name (2) 2. Constant Declaration const phi = 3.14 const Nmax = 100 const pass = ‘xyz’
Name (3) 3. Customized Type Declaration Type Point : record <x, y : real> P : Point { P is a variable referring to a type name of Point}
Naming Convensions 1. 2. 3. 4.
Begin with an alphabet, not numbers, space or special characters Using only alphabets, number or underscore ‘_’ No space; Must be declared.
Incorrect Naming !! 6Point
{ begin with number}
Student name
{ space between single name}
Hey!
{ special character}
VALUE The
value of the already-known data types (variables, constants).
Assigning value (1) 1.
Direct Assignment k5 {assign a value of ‘5’ to k} var1 var2 {assign a value from var2 to var1}
The value recorded will be the last one.
Example Declaration Type point record <x, y: real> K: integer Distance: real Found: boolean P : point
K 5 Distance 0.03 Found false P.x 5 P.y 10
K 5 distance 0.03 Found false P.x 5 P.y 10
Using expression ca+b Determinant b^2-(4*a*c) Same
name types must be used
a a+2
?
Example Declaration M,P : integer M 16 P M*2 M P + 100 P P + M
M ?? P ??
Assigning value (2) 2. Using input devices Declaration
Declaration
Type point record <x, y: real>
Type point record <x, y: real>
K: integer Distance: real Found: boolean P : point
K: integer Distance: real Found: boolean P : point
K 5 Distance 0.03 Found false P.x 5 P.y 10
Read (K) Read (Distance) Read (Found) Read (P.x, P.y)