Z14690010220164025COMP6268 - Algorithm and Programming - 01
Z14690010220164025COMP6268 - Algorithm and Programming - 01
Programming
Effective Period : February 2016
INTRODUCTION TO ALGORITHM
AND DEVELOPMENT OF COMPUTER
PROGRAM
Session 1
Learning Objectives
milk tea
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
• 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 ‘+’
• 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