340 likes | 528 Views
FTSM. Algorithm. Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution in flowchart and pseudocode forms. Algorithm in Real Life. Consider the following …. Problem: B aking a Cake How to solve: Start Preheat the oven at 180 o C
E N D
FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution in flowchart and pseudocode forms Computer Science Department
Algorithm in Real Life Consider the following …. Problem: Baking a Cake How to solve: • 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 Programming2
‘Divide and Conquer’ Strategy in Algorithm Problem: Prepare a Breakfast 1. Start 2. Prepare a Breakfast 3. End TK1913-C Programming3
‘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 Programming4
‘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 Programming5
‘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 Programming6
‘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 Programming7
Something to ponder … What is the connection between these real life processes and algorithm? TK1913-C Programming8
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 problemsolution TK1913-C Programming9
Flowchart Flowchart represents algorithm graphically. It is intended for communication and documentation TK1913-C Programming10
Symbol Semantic Start/End Process Input/Output Test Connector Flow of activities Flowchart – Basic Syntax TK1913-C Programming11
Symbol Semantic Function call Magnetic Disc Stored Data Document/File Multiple Document/File Flowchart – Other Syntax TK1913-C Programming12
Something to ponder … Are the steps in the algorithm discussed earlier specific enough to be executed by computer? TK1913-C Programming13
Problem Solving Process Process Input Output TK1913-C Programming14
Example 1 Calculate and display the price of a number of apples if the quantity in kg and price per kg are given. Process Input Output • Quantity • Price_per_kg Price = Quantity * Price_per_kg Price TK1913-C Programming15
Flowchart: Calculate Price of Apples Start Input Quantity Input Price_per_kg Price Quantity * Price_per_kg Output Price End TK1913-C Programming16
Start Input Quantity Input Price_per_kg Price Quantity * Price_per_kg Output Price End C Program: Calculate Price of Apples void main() { scanf(“%d”,&quantity); scanf(“%d”,&price_per_kg); price = quantity*price_per_kg; printf(“%d”, price); } TK1913-C Programming17
It’s not complete! Declare the variables… C Program: Calculate Price of Apples void main() { scanf(“%d”,&quantity); scanf(“%d”,&price_per_kg); price = quantity*price_per_kg; printf(“%d”, price); } TK1913-C Programming18
Well done ! But…what are they? C Program: Calculate Price of Apples void main() { int quantity, price_per_kg, price; scanf(“%d”,&quantity); scanf(“%d”,&price_per_kg); price = quantity*price_per_kg; printf(“%d”, price); } TK1913-C Programming19
Declaration Start } Input Process Output End C Program: Calculate Price of Apples void main() { int quantity, price_per_kg, price; scanf(“%d”,&quantity); scanf(“%d”,&price_per_kg); price = quantity*price_per_kg; printf(“%d”, price); } TK1913-C Programming20
Chapter 4 } Chapter 5 Chapter 6 Chapter 5 C Program: Calculate Price of Apples void main() { int quantity, price_per_kg, price; scanf(“%d”,&quantity); scanf(“%d”,&price_per_kg); price = quantity*price_per_kg; printf(“%d”, price); } TK1913-C Programming21
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 vehicle’s entry and exit time. Process Input Output • Entry_time • Exit_time ???? Charge TK1913-C Programming22
No Yes Flowchart: Calculate Car Park Charge Start Input Entry_time Input Exit_time Period Exit_time – Entry_time Period > 1? Charge 2 Charge 2 + (Period * 1) Output Charge End TK1913-C Programming23
Start Input Entry_time Input Exit_time Period Exit_time – Entry_time Period > 1? No Yes Charge 2 Charge 2 + (Period * 1) Output Charge End Flowchart: Calculate Car Park Charge scanf(“%d%d”,&entry_time,&exit_time); period = exit_time – entry_time; if (period > 1) charge = 2 + ( period *1); else charge = 2; printf(“%d”,charge); TK1913-C Programming24
} Chapter 7 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; printf(“%d”,charge); } TK1913-C Programming25
Example 3 Write a program to calculate the average mark of three TK1913’s students. Process Input Output • Mark A • Mark B • Mark C ???? Average_mark THINK!! TK1913-C Programming26
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 Programming27
Something to ponder … What might be the disadvantage of flowchart? TK1913-C Programming28
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 Programming29
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 Programming30
Refer to the handouts in your manual …. OK?? Guidelines on Writing Pseudocode TK1913-C Programming31
CFlow Demonstration TK1913-C Programming32
Yes !! That’s all? What’s next??? INTRODUCTION TO C on the way … End of Lecture 2 TK1913-C Programming33