Algorithm
Knowledge:
Understand algorithm representation using flowchart and
pseudocode
Skill:
Map problem to solution in flowchart and pseudocode forms
Computer Science Department
FTSM
Algorithm in Real Life
Consider the following .
Problem: Baking a Cake
How to solve:
1.
2.
3.
4.
5.
6.
7.
8.
Start
Preheat the oven at 180oC
Prepare a baking pan
Beat butter with sugar
Mix them with flour, eggs and essence vanilla
Pour the dough into the baking pan
Put the pan into the oven
End
TK1913-C Programming
Divide and Conquer Strategy
in Algorithm
Problem: Prepare a Breakfast
1. Start
2. Prepare a Breakfast
3. End
TK1913-C Programming
Divide and Conquer Strategy
in Algorithm
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
TK1913-C Programming
Divide and Conquer Strategy
in Algorithm
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2 Prepare some chips
2.3 Make a cup of coffee
3. End
TK1913-C Programming
Divide and Conquer Strategy
in Algorithm
1. Start
2. Prepare a Breakfast
2.1 Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2 Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
2.3 Make a cup of coffee
3. End
TK1913-C Programming
Divide and Conquer Strategy
in Algorithm
1. Start
2. Prepare a Breakfast
2.1. Prepare a tuna sandwich
2.1.1 Take 2 slices of bread
2.1.2 Prepare tuna paste
2.2. Prepare some chips
2.2.1 Cut potatoes into slices
2.2.2 Fry the potatoes
2.3. Make a cup of coffee
2.3.1 Boil water
2.3.2 Add water with sugar and coffee
3. End
TK1913-C Programming
Something to ponder
What is the connection
between these real life
processes and
algorithm?
TK1913-C Programming
Algorithm
A specific and step-by-step set of instructions
for carrying out a procedure or solving a
problem, usually with the requirement that the
procedure terminate at some point
2 types of algorithm representation will be
explained:
Flowchart
Pseudocode
Structured Method (will be explained later)
A method of designing problem solution
TK1913-C Programming
Flowchart
Flowchart represents
algorithm graphically.
It is intended for
communication and
documentation
TK1913-C Programming
10
Flowchart Basic Syntax
Symbol
Semantic
Start/End
Process
Input/Output
Test
Connector
Flow of activities
TK1913-C Programming
11
Flowchart Other Syntax
Symbol
Semantic
Function call
Magnetic Disc
Stored Data
Document/File
Multiple Document/File
TK1913-C Programming
12
Something to ponder
Are the steps in the
algorithm discussed
earlier specific
enough to be
executed by
computer?
TK1913-C Programming
13
Problem Solving Process
Input
TK1913-C Programming
Process
Output
14
Example 1
Calculate and display the price of a
number of apples if the quantity in kg and
price per kg are given.
Input
Quantity
Process
Price = Quantity * Price_per_kg
Output
Price
Price_per_kg
TK1913-C Programming
15
Flowchart: Calculate Price of
Start
Apples
Input
Quantity
Input
Price_per_kg
Price Quantity * Price_per_kg
Output
Price
End
TK1913-C Programming
16
C Program: Calculate Price of Apples
Start
Input
Quantity
Input
Price_per_kg
Price Quantity * Price_per_kg
Output
Price
End
TK1913-C Programming
void main() {
scanf(%d,&quantity);
scanf(%d,&price_per_kg);
price = quantity*price_per_kg;
printf(%d, price);
}
17
C Program: Calculate Price of Apples
void main() {
scanf(%d,&quantity);
scanf(%d,&price_per_kg);
Its not complete!
Declare the variables
price = quantity*price_per_kg;
printf(%d, price);
TK1913-C Programming
18
C Program: Calculate Price of Apples
void main() {
int quantity, price_per_kg, price;
scanf(%d,&quantity);
Well done ! Butwhat
are they?
scanf(%d,&price_per_kg);
price = quantity*price_per_kg;
printf(%d, price);
}
TK1913-C Programming
19
C Program: Calculate Price of Apples
Start
void main() {
Declaration
int quantity, price_per_kg, price;
scanf(%d,&quantity);
scanf(%d,&price_per_kg);
Input
Process
price = quantity*price_per_kg;
printf(%d, price);
}
End
TK1913-C Programming
Output
20
C Program: Calculate Price of Apples
void main() {
Chapter 4
int quantity, price_per_kg, price;
scanf(%d,&quantity);
scanf(%d,&price_per_kg);
Chapter 5
Chapter 6
price = quantity*price_per_kg;
printf(%d, price);
Chapter 5
}
TK1913-C Programming
21
Example 2
A car park has the following charges:
The 1st hour costs RM2.00. The subsequent hours
cost RM1.00 per hour. Write an algorithm based on
a vehicles entry and exit time.
Input
Entry_time
Exit_time
TK1913-C Programming
Process
Output
????
Charge
22
Flowchart: Calculate Car Park
Start
Charge
Input Entry_time
Input Exit_time
Period Exit_time Entry_time
Charge 2
No
Period > 1?
Yes
Charge 2 + (Period * 1)
Output
Charge
End
TK1913-C Programming
23
Flowchart: Calculate Car Park
Start
Charge
Input Entry_time
Input Exit_time
scanf(%d%d,&entry_time,&exit_time);
Period Exit_time Entry_time
Charge 2
No
Period > 1?
Output
Charge
End
TK1913-C Programming
Yes
period = exit_time entry_time;
Charge 2 + (Period * 1)
if (period > 1)
charge = 2 + ( period *1);
else
charge = 2;
printf(%d,charge);
24
C Program: Calculate Car Park
Charge void main() {
int entry_time, exit_time, period, charge;
scanf(%d%d,&entry_time,&exit_time);
period = exit_time entry_time;
if (period > 1)
charge = 2 + (period * 1);
else
charge = 2;
Chapter 7
printf(%d,charge);
TK1913-C Programming
25
Example 3
Write a program to calculate the average
mark of three TK1913s students.
Input
Mark A
Mark B
Mark C
TK1913-C Programming
Process
Output
????
Average_mark
THINK!!
26
Algorithm Development
Guidelines
Identify the input and output of the problem.
If necessary, use Divide & Conquer strategy
to decompose the problem into smaller and
manageable sub problems. Decompose the
problem until all the sub problems can be
solved to get the required results
For each sub problem, identify and list out the
steps involved in solving it
TK1913-C Programming
27
Something to ponder
What might be the
disadvantage of
flowchart?
TK1913-C Programming
28
Pseudocode
An outline of a program, written in a form that
can easily be converted into real programming
statements. It resembles the actual program that
will be implemented later. However, it cannot
be compiled nor executed.
Pseudocode normally codes the following
actions:
Initialisation of variables
Assignment of values to the variables
Arithmetic operations
Relational operations
TK1913-C Programming
29
Example of Pseudocode
1. Start
2. Read quantity
3. Read price_per_kg
4. price quantity * price_per_kg
5. Print price
6. End
TK1913-C Programming
30
Guidelines on Writing
Pseudocode
Refer to the handouts
in your manual . OK??
TK1913-C Programming
31
CFlow Demonstration
TK1913-C Programming
32
End of Lecture 2
Yes !! Thats all?
Whats next???
INTRODUCTION TO C
on the way
TK1913-C Programming
33