0% found this document useful (0 votes)
16 views

Z14690010220164025COMP6268 - Algorithm and Programming - 01

Uploaded by

yoelwijaya.10.92
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Z14690010220164025COMP6268 - Algorithm and Programming - 01

Uploaded by

yoelwijaya.10.92
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Course: COMP6268 – Algorithm and

Programming
Effective Period : February 2016

INTRODUCTION TO ALGORITHM
AND DEVELOPMENT OF COMPUTER
PROGRAM

Session 1
Learning Objectives

At the end of this session, student will be


able to:
• Explain algorithms in problem solving
• Design algorithm in pseudocode and flowchart
Contents
Outline
• Definition of an Algorithm
• Example of Algorithm
• History of Algorithm
• Good Algorithm Practice
• Efficient Algorithm
• Algorithm Development
• Algorithm for Solving Problem
• Algorithm Representation
• Advantages of Algorithm
• Development of a Computer Program
• Flow Chart Definition
• Symbols
• Example of Flow Chart
• Definition of Pseudocode
• Six Basic Computer Operations
Definition of an Algorithm
• A process or set of rules to be followed in calculations or
other problem-solving operations, especially by a
computer.
• A self-contained step-by-step set of operations to be
performed.
• A set of detailed, unambiguous and ordered
instructions developed to describe the processes
necessary to produce the desired output from a given
input.

Algorithm is written in simple English and is not a formal


document.
Example of Algorithm
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
Example of Algorithm
Another example, if you want to instruct someone to add
up a list of numbers on a pocket calculator, you might
write an algorithm such as the following:
Turn on calculator
Clear calculator
Repeat
Input number
Press addition (+) key
Until all numbers have been entered
Write down total number
Turn off calculator
History of Algorithm
• The term ‘algorithm' originated from the name of a
Latin translation of a book written by Al-Khwārizmī, a
Persian Mathematician.
• Al-Khwarizmi’s work on the technique of performing
arithmetic was translated into Latin as Algoritmi de
numero Indorum (in English, "Al-Khwarizmi on the
Hindu Art of Reckoning").
Good Algorithm Practice
• Be lucid, precise and unambiguous.
• Steps are written consecutively.
• Give the correct solution in all cases.
• Eventually end.
Good Algorithm Practice
Which one is better algorithm?
Cooking instant noodles Cooking instant noodles
algorithm: algorithm:
(1) Prepare a pack of (1) Prepare 100 gr of
instant noodles and a instant noodles and a
cooking tool, cooking pot,
(2) Boil the noodles in a pot (2) Boil the noodles in a pot
with water until it with 400 ml water for 3
boiled, minutes,
(3) Put the ingredients with (3) Put the ingredients with
the powder into water. the powder into water.
Efficient Algorithms
• Right logical flows
• Fast
• Save space
• Simple
• General
Algorithm Development
Steps in development of Algorithms
1. Defining the problem
2. Designing a solution algorithm
3. Checking the solution algorithm
Algorithm Development
1. Defining the problem
• Input: a list of the source data provided to the
problem.
• Output: a list of the outputs required.
• Processing: a list of actions needed to produce the
required outputs.
Algorithm Development
2. Designing a solution algorithm
• Outline solution.
• Write down the steps concisely.
• Put the steps in right order, from start to end.
• Trial-and-error process.
Algorithm Development
3. Checking the solution algorithm
• Provide sample data
• Desk check table
• Fix logic errors.
Algorithm for Solving Problem
Example 1
• Problem statement:
There are 3 glasses. First glass is filled with milk.
Second glass is filled with tea. How to swap the
contents of those two glasses with an empty
mediator glass? Write the algorithm step by step.

milk tea

Glass 1 Mediator Glass Glass 2


Algorithm for Solving Problem
Example 1
• Solution:
1. Pour milk from glass 1 into mediator glass.
2. Pour tea from glass 2 into glass 1.
3. Pour milk from mediator glass into glass 2.
Algorithm for Solving Problem
Example 2:
• Problem illustration:
Hanoi Tower

A B C
Algorithm for Solving Problem
Example 2:
• Problem statement:
Move all disks from peg A to peg C with set of rules:
– Only one disk can be moved at a time.
– Only the uppermost disk on a stack that can be
moved at each move.
– No disk may be placed on top of a smaller disk.
Write the algorithm step by step.
Algorithm for Solving Problem
Example 2
• Solution:
1. Make the legal move between pegs A and B
2. Make the legal move between pegs A and C
3. Make the legal move between pegs B and C
4. Repeat until complete
Algorithm Representation
• Description
Sentences written in simple English.
• Diagram or symbol
In form of flow chart
• Pseudocode
Outline of computer program
Advantages of Algorithm
• Why do we write algorithm first before creating a
program?
• Why don’t we just directly write the program?
• What are the consequences if we don’t write the
algorithm and just directly write the program?
Advantages of Algorithm
• Everyone can write algorithm.
• Algorithm is not bound to specific programming
language.
• Algorithm can be translated into any programming
language.
• Despite of the programming language, the expected
output is the same.
• Focus to logic, not syntax.
Development of a Computer Program
• Anyone can write well-designed programs.
• Programming can be defined as the development of
a solution to an identified problem, and the setting
up of a related series of instructions that, when
directed through computer hardware, will produce
the desired results.
Development of a Computer Program
These are basic steps in developing a program:
1. Define the problem
2. Outline the solution
3. Develop the outline into an algorithm
4. Test the algorithm for correctness
5. Code the algorithm into a specific programming
language
6. Run the program on the computer
7. Document and maintain the program
Development of a Computer Program
1. Define the problem
• Inputs
• Outputs
• Processing steps
Development of a Computer Program
2. Outline the solution
• Break down tasks
• Hierarchy or structure chart
Development of a Computer Program
3. Develop the outline into an algorithm
Outline solution expanded into:
• Algorithm
• Pseudocode
• Flow chart
Development of a Computer Program
3. Develop the outline into an algorithm
• Algorithm
A set of precise steps that describe exactly the tasks to be
performed and the order in which they are to be carried out.

• Pseudocode
A form of structured English.

• Flow chart
a diagram of the sequence of movements or actions of
people or things involved in a complex system or activity.
Development of a Computer Program
4. Test the algorithm for correctness
• Provide sample data
• Desk check table
Development of a Computer Program
5. Code the algorithm into a specific programming language

• Choose programming language


(e.g. C, C++, C#, Java, PHP, Python, etc.)
Development of a Computer Program
6. Run the program on the computer
• Test data to machine.
• Check for syntax errors and logic errors.
Development of a Computer Program
7. Document and maintain the program
• Internal documentation.
• External documentation.
Flow Chart Definition
• A diagram of the sequence of movements or actions
of people or things involved in a complex system or
activity.
• A graphical representation of a computer program in
relation to its sequence of functions (as distinct from
the data it processes).
Symbols
• Terminal symbol
Start or stopping point in the logic.
Every flowchart should begin and
end with terminal symbol.

• Input / Output symbol


Represents an input or output
process in an algorithm
Symbols
• Process symbol
Represents any single process in an
algorithm. The flow of control is
sequential.

• Predefined process symbol


Represents a module in an
algorithm – that is, a predefined
process that has its own flowchart.
Symbols
• Decision symbol
Represents a decision in the logic
involving the comparison of two
values.

• Flow lines
Connect various symbols in a
flowchart.
Example of Flow Chart
Algorithm to add up a list of numbers on a pocket
calculator:
Turn on calculator
Clear calculator
Repeat
Input number
Press addition (+) key
Until all numbers have been entered
Write down total number
Turn off calculator
Example of Flow Chart
Start

Turn on calculator

Clear calculator

Input number

Input ‘+’

T Not last F Turn off


Output
number? End
total number calculator
Definition of Pseudocode
• A notation resembling a simplified programming
language, used in program design.
• A subset of English that has been formalized and
abbreviated to look like a high-level computer
language.

Use words and phrases in the pseudocode that


correspond to some basic computer operations to
simplify translation from pseudocode algorithm to a
specific programming language.
Six Basic Computer Operations
1. A computer can receive information
• Input examples: letters typed from keyboard, mouse
click, sound from microphone, information in a file, etc.
• Verbs used in the pseudocode:
– Read
– Get
• Read is usually used to receive input from a record on a
file.
• Get is usually used to receive input from the keyboard.
Six Basic Computer Operations
1. A computer can receive information
• Example:
Read student_name
Read number_1, number_2
Get price
Get product_code
Each example uses a single verb followed by one or
more nouns to indicate what data is to be obtained.
Six Basic Computer Operations
2. A computer can put out information
• Output examples: letters displayed on screen, sound
from speakers, information recorded in a file, etc.
• Verbs used in the pseudocode:
– Print
– Write
– Put
– Output
– Display
– Prompt
Six Basic Computer Operations
2. A computer can put out information
• Print is usually used to send output to the printer.
• Write is usually used to write output to a file.
• Put, Output or Display are used to write output to the screen.
• Example:
Print ‘Program Completed’
Write customer record to master file
Put out name, address and postcode
Output total_tax
Display ‘End of data’
Six Basic Computer Operations
2. A computer can put out information
• Prompt instruction is usually used before an input Get
instruction.
• Example:
Prompt for student_mark
Get student_mark
Six Basic Computer Operations
3. A computer can perform arithmetic
• Use either actual mathematical symbols or the words
for those symbols.
• Symbols:
+ for add
- for subtract
* for multiply
/ for divide
() for parentheses
= for equal or assignment of a value
Six Basic Computer Operations
3. A computer can perform arithmetic
• Example of same pseudocode instruction:
add number to total
total = total + number

• Other examples:
divide total_marks by student_count
sales_tax = cost_price * 0.10
Six Basic Computer Operations
3. A computer can perform arithmetic
• Verbs Compute and Calculate are also available.
Examples:
compute C = (F – 32) * 5/9
calculate service_tax = total_price * 0.5

• When writing mathematical operations, remember


symbol precedence that will affect order of
operations executed.
Six Basic Computer Operations
4. A computer can assign a value to a variable or memory
location
• Assign a value to a variable using:
– Initialise or Set to give initial value.
– Symbols ‘=‘ or ‘←’ to assign a result of some
processing.
– Save or Store to keep a variable for later use.
Six Basic Computer Operations
4. A computer can assign a value to a variable or memory
location
• Examples:
Initialise total_price to zero
Set student_count to 0
total_price = cost_price + sales_tax
total_price ← cost_price + sales_tax
store customer_num in last_customer_num
Six Basic Computer Operations
5. A computer can compare two variables and select one of two
alternative actions
• Keywords used:
– IF
– THEN
– ELSE
– ENDIF
Six Basic Computer Operations
5. A computer can compare two variables and select one of two
alternative actions
• Example:
IF total_buy is greater than 100000 THEN
set discount to 9000
ELSE
set discount to 0
ENDIF
Six Basic Computer Operations
6. A computer can repeat a group of actions
• Keywords used:
– DOWHILE
– ENDDO
Six Basic Computer Operations
6. A computer can repeat a group of actions
• Example:
DOWHILE student_total < 50
Read student_record
Print student_name, address to report
add 1 to student_total
ENDDO
Variable Names
• Should be unique.
• Meaningful.
• If more than one word is used in the name of a
variable, use underscores or capital letter as word
separators. For example: sales_tax or salesTax.
References
Books
• Robertson, L. A. (2006). Simple Program Design A Step-
By-Step Approach. Australia: Cengage Learning.
Chapter 1, 2, 3, Appendix 1.
• Deitel, P. & Deitel, H. (2010). C How to Program (6th
ed.). New Jersey: Pearson Education, Inc. Chapter 1
(pp. 7-8), Chapter 3 (p. 55).
Websites
• http://www.oxforddictionaries.com/definition/englis
h/algorithm
• https://en.wikipedia.org/wiki/Algorithm
• https://en.wikipedia.org/wiki/Tower_of_Hanoi
• http://www.oxforddictionaries.com/definition/englis
h/flow-chart

You might also like