Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

Similar presentations


Presentation on theme: "Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution."— Presentation transcript:

1 Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution in flowchart and pseudocode forms

2 TK1913-C Programming2 TK1913-C Programming 2 Consider the following …. Problem: Baking a Cake How to solve: 1. Start 2. Preheat the oven at 180 o C 3. Prepare a baking pan 4. Beat butter with sugar 5. Mix them with flour, eggs and essence vanilla 6. Pour the dough into the baking pan 7. Put the pan into the oven 8. End Algorithm in Real Life

3 TK1913-C Programming3 TK1913-C Programming 3 Divide and Conquer Strategy in Algorithm Problem: Prepare a Breakfast 1.Start 2.Prepare a Breakfast 3.End

4 TK1913-C Programming4 TK1913-C Programming 4 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 Divide and Conquer Strategy in Algorithm

5 TK1913-C Programming5 TK1913-C Programming 5 1. Start 2. Prepare a Breakfast 2.1 Prepare a tuna sandwich Take 2 slices of bread Prepare tuna paste 2.2 Prepare some chips 2.3 Make a cup of coffee 3. End Divide and Conquer Strategy in Algorithm

6 TK1913-C Programming6 TK1913-C Programming 6 1. Start 2. Prepare a Breakfast 2.1 Prepare a tuna sandwich Take 2 slices of bread Prepare tuna paste 2.2 Prepare some chips Cut potatoes into slices Fry the potatoes 2.3 Make a cup of coffee 3. End Divide and Conquer Strategy in Algorithm

7 TK1913-C Programming7 TK1913-C Programming 7 1. Start 2. Prepare a Breakfast 2.1. Prepare a tuna sandwich Take 2 slices of bread Prepare tuna paste 2.2. Prepare some chips Cut potatoes into slices Fry the potatoes 2.3. Make a cup of coffee Boil water Add water with sugar and coffee 3. End Divide and Conquer Strategy in Algorithm

8 TK1913-C Programming8 TK1913-C Programming 8 What is the connection between these real life processes and algorithm? Something to ponder …

9 TK1913-C Programming9 TK1913-C Programming 9 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 Algorithm

10 TK1913-C Programming10 TK1913-C Programming 10 Flowchart represents algorithm graphically. It is intended for communication and documentation Flowchart

11 TK1913-C Programming11 TK1913-C Programming 11 Flowchart – Basic Syntax Start/EndSymbolSemantic Process Input/Output Test Connector Flow of activities

12 TK1913-C Programming12 TK1913-C Programming 12 Flowchart – Other Syntax Function callSymbolSemantic Magnetic Disc Stored Data Document/File Multiple Document/File

13 TK1913-C Programming13 TK1913-C Programming 13 Are the steps in the algorithm discussed earlier specific enough to be executed by computer? Something to ponder …

14 TK1913-C Programming14 TK1913-C Programming 14 Problem Solving Process Input Process Output

15 TK1913-C Programming15 TK1913-C Programming 15 Example 1 Calculate and display the price of a number of apples if the quantity in kg and price per kg are given. Quantity Price_per_kg Price_per_kgPrice Price = Quantity * Price_per_kg Input Process Output

16 TK1913-C Programming16 TK1913-C Programming 16 Flowchart: Calculate Price of Apples InputQuantity Start Price Quantity * Price_per_kg InputPrice_per_kg OutputPrice End

17 TK1913-C Programming17 TK1913-C Programming 17 void main() { scanf(%d,&quantity); scanf(%d,&price_per_kg); price = quantity*price_per_kg; printf(%d, price); } InputQuantity Start Price Quantity * Price_per_kg InputPrice_per_kg OutputPrice End C Program: Calculate Price of Apples

18 TK1913-C Programming18 TK1913-C Programming 18 void main() { scanf(%d,&quantity); scanf(%d,&price_per_kg); price = quantity*price_per_kg; printf(%d, price); } C Program: Calculate Price of Apples Its not complete! Declare the variables…

19 TK1913-C Programming19 TK1913-C Programming 19 void main() { int quantity, price_per_kg, price; scanf(%d,&quantity); scanf(%d,&price_per_kg); price = quantity*price_per_kg; printf(%d, price); } Well done ! But…what are they? C Program: Calculate Price of Apples

20 TK1913-C Programming20 TK1913-C Programming 20 void main() { int quantity, price_per_kg, price; scanf(%d,&quantity); scanf(%d,&price_per_kg); price = quantity*price_per_kg; printf(%d, price); }Start Declaration }Input Process Output End C Program: Calculate Price of Apples

21 TK1913-C Programming21 TK1913-C Programming 21 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); }

22 TK1913-C Programming22 TK1913-C Programming 22 Example 2 A car park has the following charges: The 1 st hour costs RM2.00. The subsequent hours cost RM1.00 per hour. Write an algorithm based on a vehicles entry and exit time. Entry_time Exit_time Exit_time Charge???? Input Process Output

23 TK1913-C Programming23 TK1913-C Programming 23 Flowchart: Calculate Car Park Charge Input Entry_time Input Exit_time Start OutputCharge End Period Exit_time – Entry_time Period > 1? Yes Charge 2 + (Period * 1) Charge 2 No

24 TK1913-C Programming24 TK1913-C Programming 24 scanf(%d%d,&entry_time,&exit_time); period = exit_time – entry_time; Input Entry_time Input Exit_time Start OutputCharge End Period Exit_time – Entry_time Period > 1? Yes Charge 2 + (Period * 1) Charge 2 No if (period > 1) charge = 2 + ( period *1); else charge = 2; printf(%d,charge); Flowchart: Calculate Car Park Charge

25 TK1913-C Programming25 TK1913-C Programming 25 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); } } Chapter 7 C Program: Calculate Car Park Charge

26 TK1913-C Programming26 TK1913-C Programming 26 Write a program to calculate the average mark of three TK1913s students. Example 3 Mark A Mark B Mark B Mark C Mark C Average_mark???? Input Process Output THINK!!

27 TK1913-C Programming27 TK1913-C Programming 27 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 Algorithm Development Guidelines

28 TK1913-C Programming28 TK1913-C Programming 28 What might be the disadvantage of flowchart? Something to ponder …

29 TK1913-C Programming29 TK1913-C Programming 29 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 Pseudocode

30 TK1913-C Programming30 TK1913-C Programming Start quantity 2. Read quantity price_per_kg 3. Read price_per_kg 4. pricequantityprice_per_kg 4. price quantity * price_per_kg price 5. Print price 6. End Example of Pseudocode

31 TK1913-C Programming31 TK1913-C Programming 31 Guidelines on Writing Pseudocode Refer to the handouts in your manual …. OK??

32 TK1913-C Programming32 TK1913-C Programming 32 CFlow Demonstration

33 TK1913-C Programming33 TK1913-C Programming 33 End of Lecture 2 Yes !! Thats all? Whats next??? INTRODUCTION TO C on the way …


Download ppt "Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution."

Similar presentations


Ads by Google