Subject : COMP6047
ALGORITHM AND PROGRAMMING
Year : 2019
Algorithm & Programming
Learning Outcomes
At the end of this session, student will be able to:
• Define algorithm theory and design (LO1)
COMP6047 - Algorithm and Programming 2
Sub Topics
Algorithm and Programming:
– Algorithm Definition
– Algorithm Development Steps
– Pseudo-code
– Flow Chart
– Structured Theorem
– Exercise
COMP6047 - Algorithm and Programming 3
Algorithm Definition
• Algorithm is a procedure for solving a problem in terms of the
actions to be executed, and the order in which these actions
are to be executed
• Derived from the word algoris and ritmis. Introduced by Al-
Khowarizmi.
• In the programming domain, algorithm define as method that
consist of structured steps in problem solving using computer.
COMP6047 - Algorithm and Programming 4
Simple Algorithm Example
Rise and Shine Algorithm
(1)Get out of bed
(2)Take off pajamas
(3)Take a shower
(4)Get dressed
(5)Eat breakfast
(6)Carpool to work
COMP6047 - Algorithm and Programming 5
Algorithm Development Steps
PROBLEM PROCESS SOLUTION
Algorithm Source Code Executable Code
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
6
Documentation
COMP6047 - Algorithm and Programming
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Problem:
Syntax Err
Finding the solution or root of quadratic
equation
Executable code:
=> Run
Definition:
Quadratic equation : ax^2 + bx + c = 0
Output Err
Data needed:
Coefficient of a, b and c : real type
Documentation
COMP6047 - Algorithm and Programming 7
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Mathematical model:
Quadratic formula:
Executable code:
x1 = (-b + sqrt(b^2 - 4ac))/2a => Run
x2 = (-b – sqrt(b^2 - 4ac))/2a
Output Err
Documentation
COMP6047 - Algorithm and Programming 8
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
Start COMPILE
Input a,b,c
Syntax Err
d = b^2 – 4ac
Executable code:
=> Run
Y
d<0
T Output Err
x1=(-b+sqrt(d))/2a Print:
x2 =(-b-sqrt(d))/2a “Imaginary”
Documentation
Print : x1, x2
Stop COMP6047 - Algorithm and Programming 9
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
COMP6047 - Algorithm and Programming 10
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
COMP6047 - Algorithm and Programming 11
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
COMP6047 - Algorithm and Programming 12
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
COMP6047 - Algorithm and Programming 13
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
COMP6047 - Algorithm and Programming 14
Algorithm Development Steps
Problem Model Algorithm Writing Code
Definition Development Design
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
COMP6047 - Algorithm and Programming 15
Representing Algorithm
• How to develop an algorithm?
We can use:
– Writing
Structure English and Pseudo-code.
– Drawing
Flow Chart
COMP6047 - Algorithm and Programming 16
Pseudo-code
• An artificial and informal language that helps you develop
algorithms
• Pseudo-code is similar to everyday English, convenient, and user
friendly
• Keywords are used to describe control structure
Example:
if, else, print, set, add, while, etc.
COMP6047 - Algorithm and Programming 17
Pseudo-code
Basic Computer Operation:
1. Input
2. Output
3. Compute
4. Storing value to an identifier (Store)
5. Compare (Selection)
6. Repetition (Loop)
COMP6047 - Algorithm and Programming 18
1. Input
• Statements can be used when a computer receive information or
input
Read, Get, Input or Key-In
• Example:
Read bilangan
Get tax_code
Baca students_name
COMP6047 - Algorithm and Programming 19
2. Output
• Statements can be used when a computer displaying information
or output:
Print, Write, Put, Output, or Display
• Example:
Print “Bina Nusantara University”
Write “Algorithm and Programming”
Output Total
COMP6047 - Algorithm and Programming 20
3. Compute
• To do arithmetic calculation the following operators are used:
+(add)
- (subtract)
* (multiply)
/ (divide)
() (scope)
• Statement Compute, Calculate or Add also can be used
• Example:
Add number to total
Total = Total + number
COMP6047 - Algorithm and Programming 21
4. Storing Value to An Identifier (Store)
• There are three ways of storing value into a variable:
– Initializing value using statement Initialize or
Set
– Storing value as calculation result using =
– To simply store a value into a variable using
“Save” or Store
• Example:
Set Counter to 0
Total = Price * Qty
COMP6047 - Algorithm and Programming 22
5. Compare
• One of the main operation in computing is comparing values and
choosing options based on its result
• Keyword used: IF, THEN and ELSE
• Example:
IF Menu=‘1’ THEN
Discount = 0.1 * price
ELSE
Discount = 0.2 * price
ENDIF
COMP6047 - Algorithm and Programming 23
6. Repetition (Looping)
• To repeat an action/step, we use keyword DOWHILE and ENDDO
• Example:
DOWHILE number < 10
print number
number = number +1
ENDDO
COMP6047 - Algorithm and Programming 24
Pseudo-code Example
Example : Algorithm using a calculator to sum values
Start
Set the calculator ON
Empty any values
Do
Input price
Push plus button (+)
while all prices have been input
print total price
turn OFF calculator
End
COMP6047 - Algorithm and Programming 25
Pseudo-code Example
Example : Algorithm to count average grade of a class
Start
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Add one to the grade counter
Set the class average to the total divided by ten
Print the class average.
End
COMP6047 - Algorithm and Programming 26
Flow Chart
COMP6047 - Algorithm and Programming 27
Flow Chart Example
COMP6047 - Algorithm and Programming 28
Good Algorithm Practice
• Having the right logical flow to solve the problem
• Producing the correct output in a time efficient manner
• Written using unambiguous structured language
• Easy implementation into real programming language
• All steps and operations are clearly defined and ended
COMP6047 - Algorithm and Programming 29
Structure Theorem
Structure theorem which makes the computer programming
possible using only three control structure, which are:
1. Sequence
2. Selection
3. Repetition
COMP6047 - Algorithm and Programming 30
1. Sequence
• Sequence is series of consecutive commands/statements
• Commonly programming language has sequence of statements
flowing from top of the program to its end
COMP6047 - Algorithm and Programming 31
1. Sequence
• Example :
Print “Number of students:”
Set total to 49
Print “Add new student:”
Read newStudent
total = total + newStudent
Print “Number of students:”
Print total
• Description
Sequence of command is from the 1st line to the end of code. If
newStudent input is 2 then total that later on printed out is 51
COMP6047 - Algorithm and Programming 32
2. Selection
• Selection control structure is structure that allow us to choose
from several options of statement/command
• The first statement will be executed if the condition is satisfied,
if not then the else statement will be executed (if the other
exist)
COMP6047 - Algorithm and Programming 33
2. Selection
• Example :
IF Day=1 THEN
Print “Monday”
ELSE
Print “Obviously not Monday”
• Description
The word “Monday” will be printed out if Day’s value equal
to 1, else it will print out the sentence “Obviously not
Monday”.
COMP6047 - Algorithm and Programming 34
3. Repetition
• A number of statements/commands can be repeated several
times using Repetition structure control
• Statements/commands will be repeated while the looping
condition is satisfied
(may use DOWHILE – ENDDO)
COMP6047 - Algorithm and Programming 35
3. Repetition
• Example :
Stars = 0
DOWHILE Stars < 5
Print Stars
Stars = Stars + 1
ENDDO
• Description
At first Stars’ value equals to 0, after following the DOWHILE
looping Stars’ value will be updated 5 times resulting:
01234
COMP6047 - Algorithm and Programming 36
Exercise
1. Using the Pseudo-code, create:
a. an algorithm to calculate a rectangle area!
b. an algorithm to change second into hour and minute
unit!
c. an algorithm to decide whether an input number is an
odd or even number!
d. an algorithm to calculate a circle area!
e. an algorithm to accept three numbers and find the max
number!
2. Repeat no. 1 using Flow Chart
COMP6047 - Algorithm and Programming 37
Summary
• Algorithm is a procedure for solving a problem in terms of the
actions to be executed
• Algorithm development steps consists of: problem definition,
model development, algorithm design, writing code, and
documentation
• We can use writing (Structure English and Pseudo-code) or
drawing (Flow Chart) to represent algorithm
• Basic Computer Operation: input, output, compute, store,
compare, and repetition (loop)
• Structure theorem are sequence, selection, and repetition
COMP6047 - Algorithm and Programming 38
References
• Paul Deitel & Harvey Deitel. (2016). C how to program : with an
introduction to C++. 08. Pearson Education. Hoboken. ISBN:
9780133976892. Chapter 3
• Programming in C: http:// www.cs.cf.ac.uk/Dave/C/
• C Language Tutorial:
http://www.physics.drexel.edu/courses/Comp_Phys/General/C_b
asics/
• Pseudocode Examples:
http://www.unf.edu/~broggio/cop2221/2221pseu.htm
• Computer & Internet Help : Understanding Flowchart Symbols:
http://www.youtube.com/watch?v=xLoL7tlJYws
COMP6047 - Algorithm and Programming 39
END
COMP6047 - Algorithm and Programming 40