Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Department

Similar presentations


Presentation on theme: "Computer Science Department"— Presentation transcript:

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

2 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 Programming

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

4 ‘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

5 ‘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

6 ‘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

7 ‘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

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

9 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

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

11 Flowchart – Basic Syntax
Start/End Symbol Semantic Process Input/Output Test Connector Flow of activities TK1913-C Programming

12 Flowchart – Other Syntax
Function call Symbol Semantic Magnetic Disc Stored Data Document/File Multiple Document/File TK1913-C Programming

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

14 Problem Solving Process
Input Process Output 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. Input Process Output Quantity Price_per_kg Price = Quantity * Price_per_kg Price TK1913-C Programming

16 Flowchart: Calculate Price of Apples
Start Input Quantity Input Price_per_kg Price  Quantity * Price_per_kg Output Price End TK1913-C Programming

17 C Program: Calculate Price of Apples
Input Quantity Start Price  Quantity * Price_per_kg Price_per_kg Output Price End void main() { scanf(“%d”,&quantity); scanf(“%d”,&price_per_kg); price = quantity*price_per_kg; printf(“%d”, price); } TK1913-C Programming

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

19 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); } Well done ! But…what are they? TK1913-C Programming

20 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); } Start Declaration } Input Process Output End TK1913-C Programming

21 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); } Chapter 4 } Chapter 5 Chapter 6 Chapter 5 TK1913-C Programming

22 Example 2 ???? Process 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. Input Process Output Entry_time Exit_time ???? Charge TK1913-C Programming

23 Flowchart: Calculate Car Park Charge
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 TK1913-C Programming

24 Flowchart: Calculate Car Park Charge
Input Entry_time Input Exit_time Start Output Charge End Period  Exit_time – Entry_time Period > 1? Yes Charge  2 + (Period * 1) Charge 2 No 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 Programming

25 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); } } Chapter 7 TK1913-C Programming

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

27 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

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

29 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

30 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

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

32 CFlow Demonstration TK1913-C Programming

33 End of Lecture 2 Yes !! That’s all? What’s next???
INTRODUCTION TO C on the way … TK1913-C Programming


Download ppt "Computer Science Department"

Similar presentations


Ads by Google