Slide 02 - Algorithm
Slide 02 - Algorithm
Knowledge:
Understand algorithm representation using flowchart and
pseudocode
Skill:
Map problem to solution in flowchart and pseudocode forms
FTSM
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
TK1913-C Programming
Something to ponder
What is the connection
between these real life
processes and
algorithm?
TK1913-C Programming
Algorithm
Flowchart
Pseudocode
TK1913-C Programming
Flowchart
Flowchart represents
algorithm graphically.
It is intended for
communication and
documentation
TK1913-C Programming
10
Semantic
Start/End
Process
Input/Output
Test
Connector
Flow of activities
TK1913-C Programming
11
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
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
Output
Price
End
TK1913-C Programming
16
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
scanf(%d,&quantity);
scanf(%d,&price_per_kg);
price = quantity*price_per_kg;
printf(%d, price);
TK1913-C Programming
18
scanf(%d,&price_per_kg);
price = quantity*price_per_kg;
printf(%d, price);
}
TK1913-C Programming
19
void main() {
Declaration
Input
Process
price = quantity*price_per_kg;
printf(%d, price);
}
End
TK1913-C Programming
Output
20
Chapter 4
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
Charge 2
No
Period > 1?
Yes
Charge 2 + (Period * 1)
Output
Charge
End
TK1913-C Programming
23
scanf(%d%d,&entry_time,&exit_time);
Charge 2
No
Period > 1?
Output
Charge
End
TK1913-C Programming
Yes
Charge 2 + (Period * 1)
if (period > 1)
charge = 2 + ( period *1);
else
charge = 2;
printf(%d,charge);
24
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
27
Something to ponder
What might be the
disadvantage of
flowchart?
TK1913-C Programming
28
Pseudocode
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