Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm development

Similar presentations


Presentation on theme: "Algorithm development"— Presentation transcript:

1 Algorithm development
Chapter 6 Algorithm development CSC118 Fundamentals of Algorithm Development

2 CSC118 Fundamentals of Algorithm Development
Lesson Outcome At the end of this chapter, students should be able to: Understand what it means by top down and bottom up design Apply top down and bottom up approaches in real world problems Construct algorithm to solve problem modularly Apply modular mechanism to pass information between modules Apply searching module Understand sorting algorithm CSC118 Fundamentals of Algorithm Development

3 CSC118 Fundamentals of Algorithm Development
Introduction To get a complete solution for a complex problem required tremendous effort and planning. To handle a complex problem, two types of design approaches can be apply: Top down design / stepwise refinement Bottom up design CSC118 Fundamentals of Algorithm Development

4 Top Down Design Approach
Top down design is an approach of breaking down a big or complex problem into a few smaller problems. The solution for each small problem is attain independently. CSC118 Fundamentals of Algorithm Development

5 Top Down Design Approach (cont.)
Top down design approach can be showed as follows: Complex Problem Sub-Problem A Algorithm A Sub-Problem B Sub-sub-Problem B1 Algorithm B1 Sub-sub-Problem B2 Algorithm B2 Sub-Problem C Sub-sub Problem C1 Algorithm C1 Sub-sub-Problem C2 Algorithm C2 CSC118 Fundamentals of Algorithm Development

6 Top Down Design Approach (cont.)
An example to explain about top down design approach is by using the ATM machine analogy. The ATM is a machine that does the following activities: Withdraw the money Check balance Pay loans Transfer money Each subsystem is then refined to get the solution. Sometimes, the subsystem is divide into another few smaller sub problems, until the entire specification is reduced to base elements. The activities at the ATM machine can further details into: CSC118 Fundamentals of Algorithm Development

7 Top Down Design Approach (cont.)
The activities at the ATM machine can further details into: When the problem is handled by a team, then the team is divided into four groups. Each activity will be handled by each group. The first group handles withdraw money. The second group handles check balance. The third group handles pay loans and the last group handles transfer money. Withdraw money Current Account Savings Account Check balance Pay loans Car Housing Personal Transfer money Own Account 3rd Party Account CSC118 Fundamentals of Algorithm Development

8 Advantages of Top Down Design Approach
Breaking the problem into smaller problems help to clarify what needs to be done. At each step of refinement, the new sub problems become less complicated and therefore, it is easier to figure out and can be easily documented and coded. Parts of the solution may turn out to be reusable. Breaking the problem into parts allows more than one person to work on the solution. CSC118 Fundamentals of Algorithm Development

9 Bottom Up Design Approach
Bottom up design is where we have to gather or integrate all the algorithms of all sub problems to get the complete algorithm. In some circumstances, we may find that we do not yet complete the algorithm for a certain sub problem. Thus, we cannot integrate all the algorithms to get the whole solution. To overcome this problem, we have to create a stub to replace the incomplete algorithm. The purpose of stub is to make sure that we can execute the entire solution. CSC118 Fundamentals of Algorithm Development

10 Bottom Up Design Approach (cont.)
For example, let’s say we break the problem into three parts, X, Y and Z. Assume that we have completed the algorithm only for X and Y. It means we cannot integrate all the solutions because of incomplete algorithm for Z. To test that the algorithm for X and Y are correct, we have to replace algorithm for part Z with a stub, then we can test our entire system. CSC118 Fundamentals of Algorithm Development

11 Bottom Up Design Approach (cont.)
Complex Problem Sub-Problem A Sub-Problem B Sub-Problem C Sub-sub-Problem B1 Sub-sub-Problem B2 Sub-sub Problem C1 Sub-sub-Problem C2 A complete solution = Algorithm A + Algorithm B1 + Algorithm B2 + Algorithm C1 + Algorithm C2 CSC118 Fundamentals of Algorithm Development

12 Skeleton for Pseudocode
When the algorithm uses modules, the algorithm consists of two parts: main module – known as calling module module(s) – known as called module The explanation of a module is known as module definition. It consists of a module header and a module body (all actions in a module). CSC118 Fundamentals of Algorithm Development

13 Skeleton for Pseudocode (cont.)
Call subProblem 1(), call subProblem 2() and call subProblem n() are known as calling module statement in a main module. It means the main module uses these modules. CSC118 Fundamentals of Algorithm Development

14 CSC118 Fundamentals of Algorithm Development
Example 6.1 Display the following diagram: ******* $ $ $ $ $ $ $ $ $ $ CSC118 Fundamentals of Algorithm Development

15 CSC118 Fundamentals of Algorithm Development
Example 6.1 (cont.) Analysis: The diagram can be divided into two different diagrams as follows: Diagram 1: ******* ******* Diagram 2: $ $ $ $ $ $ $ $ $ $ CSC118 Fundamentals of Algorithm Development

16 CSC118 Fundamentals of Algorithm Development
Example 6.1 (cont.) The first statement in the main module will be executed, Call Diagram1(). It causes all statements in the body of module Diagram1 to be executed. Then the second statement in the main module will be executed, Call Diagram2(). It causes all statements in the body of module Diagram2 to be executed CSC118 Fundamentals of Algorithm Development

17 CSC118 Fundamentals of Algorithm Development
Example 6.1 (cont.) The last statement in each module is Return. It means that after all statements in the module were executed, the control execution of statement goes back to the main module. Execution of algorithm starts at the main module When a calling module statement is found, the execution of statements goes into the called module. At return statement, the control execution of statement goes back to the main module. CSC118 Fundamentals of Algorithm Development

18 CSC118 Fundamentals of Algorithm Development
Example 6.1 (cont.) The flowchart: CSC118 Fundamentals of Algorithm Development

19 CSC118 Fundamentals of Algorithm Development
Example 6.1 (cont.) CSC118 Fundamentals of Algorithm Development

20 CSC118 Fundamentals of Algorithm Development

21 Execution of Algorithm using Module without Parameter
Draw a flowchart and trace to get the expected screen.

22 CSC118 Fundamentals of Algorithm Development
How to Name a Module(s) It is similar to naming a variable. The name of a module should give the idea what is actually done inside the module. Example, let say we want to create a module which is to find the maximum value, then the suggested name is findMax() or calculateMaximum(). The symbol () is use after the module’s name. CSC118 Fundamentals of Algorithm Development

23 Examples of Problem Solving using Module
When we want to write a pseudocode that uses modules, the steps to follow are: Write a basic algorithm Convert basic algorithm as a main module Convert each process (complex or relatively complex) into a module. Do not convert a simple process into a module CSC118 Fundamentals of Algorithm Development

24 Examples of Problem Solving using Module
Task: Calculate the area of a circle Step 1: The basic algorithm is: CSC118 Fundamentals of Algorithm Development

25 Examples of Problem Solving using Module (cont.)
Step 2: There is one process that can be done in a module as follows: CSC118 Fundamentals of Algorithm Development

26 Examples of Problem Solving using Module (cont.)
Step 3: elaborate the module as follows: CSC118 Fundamentals of Algorithm Development

27 Examples of Problem Solving using Module (cont.)
Step 4: Combine all solution in an algorithm as follows: CSC118 Fundamentals of Algorithm Development

28 Examples of Problem Solving using Module (cont.)
Flowchart: CSC118 Fundamentals of Algorithm Development

29 CSC118 Fundamentals of Algorithm Development
Exercise 6b Display the following menu: Ask the user to enter a choice and perform the appropriate operation. Assume that the data entered is correct. Choice Operation R Calculate and display the area of a rectangle T Calculate and display the perimeter of a triangle C Calculate and display the volume of a cylinder CSC118 Fundamentals of Algorithm Development

30 Formal and Actual Parameter
Parameter is a special variable used to pass data between main module and other module(s). Parameter also known as argument. There are two types of parameter: Formal parameter A variable declared in the header of module Actual parameter A variable listed in a calling module statement CSC118 Fundamentals of Algorithm Development

31 Formal and Actual Parameter (cont.)
Formal Parameter When using the actual and formal parameter(s), the number of those parameter(s) must be equal. In other words, if the number of formal parameter is m, thus the number of actual parameter is also m. CSC118 Fundamentals of Algorithm Development

32 Formal and Actual Parameter (cont.)
Based on the pseudocode (Slide 31), the main module will supply the values of variable no1 and no2 to the module findSum(). The actual values supplied by the main module is called actual parameter. In other words, the values of no1 and no2 are supplied by the main module and in to the module findSum(). These formal parameters are called in formal parameter or value formal parameters. The word in is used in front of these formal parameters. CSC118 Fundamentals of Algorithm Development

33 Formal and Actual Parameter (cont.)
There are also some times, when the module supplies values back to the main module. This formal parameter is called out formal parameter. For this case, the value is going out from the module. There are also situation where the main module supplies values to the module, do some changes to that values and return back the new values to the main module. This type of formal parameter is called in-out formal parameters. CSC118 Fundamentals of Algorithm Development

34 CSC118 Fundamentals of Algorithm Development
When to use Parameter It is important to determine when to use the parameter(s) and which type of formal parameter(s) to be used. Parameter has to be used in three situation: Send data from calling module into the called module  in formal parameter Send data from called module into the calling module  out formal parameter Send data from calling module into the called module. Do some changes to the data and send back to the calling module  in-out formal parameter CSC118 Fundamentals of Algorithm Development

35 When to use Parameter (cont.)
Sometimes the module does not need any parameter. This happen when: No data are needed to pass from calling module to called module In a situation where all data are needed to do activities in a called module is already known. In a situation where all data are needed to do activities in a called module is inside the called module. CSC118 Fundamentals of Algorithm Development

36 CSC118 Fundamentals of Algorithm Development
In Formal Parameter Trace and display the output of the following pseudocode: CSC118 Fundamentals of Algorithm Development

37 In Formal Parameter (cont.)
Problem solving using in formal parameter Task: Calculate the area of a circle Write a module that receives the radius of a circle. Then calculate and display the area of the circle. Write a main module that requires the user to enter the radius of a circle. Use the above module as defined in a) to get the area of a circle. CSC118 Fundamentals of Algorithm Development

38 In Formal Parameter (cont.)
CSC118 Fundamentals of Algorithm Development

39 CSC118 Fundamentals of Algorithm Development

40 CSC118 Fundamentals of Algorithm Development
Exercise 6c A local restaurant owner wants to automate his breakfast billing system. Assume that the restaurant offers the following breakfast items: Items Code Items Price (RM) A Plain egg 1.50 B Muffin 2.00 C French toast 2.50 CSC118 Fundamentals of Algorithm Development

41 CSC118 Fundamentals of Algorithm Development
Exercise 6c (cont.) Write a pseudocode and draw a flowchart that contains the following module: Module displayMenu() that display the menu offered by the restaurant. Module calculateTotal() that receives the item price and the amount of item. Then, calculates the total price to be paid by the customer and display the result. The total price should include a 6% tax. Write a main module that requires the user to input the item code selected by the customer and the amount of items purchased from the menu. Finally, compute and display the total price to be paid by the customer using the modules in a) and b). CSC118 Fundamentals of Algorithm Development

42 CSC118 Fundamentals of Algorithm Development
Out Formal Parameter Problem solving using out formal parameter Task: Calculate the new selling price of an item. Write a module calcPriceAfterDiscount() that will receive item’s current price . This module will return price after discount. The discount rate is 10%. Write a module calcTax() that will receive the price after discount. This module will return tax at 6% rate. Write main module that will accept current price from user. In this main module, execute all the above module in order to get the price after discount and tax. Then calculate and display the total price. CSC118 Fundamentals of Algorithm Development

43 Out Formal Parameter (cont.)
Step 1: A basic algorithm is as follows: CSC118 Fundamentals of Algorithm Development

44 Out Formal Parameter (cont.)
Step 2: Write main module based on the basic algorithm as follows: The above main module uses variables currentPrice and totalPrice. The value of the variable newPrice and tax is obtained from the module calcPriceAfterDiscount() and module calcTax(). CSC118 Fundamentals of Algorithm Development

45 Out Formal Parameter (cont.)
Step 2: Elaborate both modules, calcPriceAfterDiscount() and calcTax(). To define a module calcPriceAfterDiscount(), look at the calling module statement in the main module: Call calcPriceAfterDiscount(currentPrice, newPrice) This calling module statement uses two actual parameter currentPrice and newPrice. The module calcPriceAfterDiscount() should have two formal parameter, let say named currP and newP. The values currP is copied from currentPrice. The formal parameters currP is type of in formal parameter. The value newPrice is obtained from the formal parameter newP. It means that the newP is type of out formal parameter. CSC118 Fundamentals of Algorithm Development

46 Out Formal Parameter (cont.)
The definition of the module calcPriceAfterDiscount() is: CSC118 Fundamentals of Algorithm Development

47 Out Formal Parameter (cont.)
To define a module calcTax(), look at the calling module statement in the main module: Call calcTax(newPrice, tax) This calling module statement uses two actual parameter newPrice and tax. The module calcTax() should have two formal parameter, let say named price and totTax. The values price is copied from newPrice. The formal parameters price is type of in formal parameter. The value tax is obtained from the formal parameter totTax. It means that the totTax is type of out formal parameter. CSC118 Fundamentals of Algorithm Development

48 Out Formal Parameter (cont.)
The definition of the module calcTax() is: CSC118 Fundamentals of Algorithm Development

49 Out Formal Parameter (cont.)
Step 4: Write a complete algorithm CSC118 Fundamentals of Algorithm Development

50 Out Formal Parameter (cont.)
The relationship between the main module and modules are shown below: CSC118 Fundamentals of Algorithm Development

51 CSC118 Fundamentals of Algorithm Development

52 CSC118 Fundamentals of Algorithm Development

53 CSC118 Fundamentals of Algorithm Development
Exercise 6d Write a pseudocode that contains the following modules: Module highestNumber() that receives two integer number as a parameter and return the highest number. Module display() that receives two integer numbers and highest number as parameter. Then, it will print the two integer numbers and the highest number. Main module that ask the user to enter two integer numbers and call the above modules. CSC118 Fundamentals of Algorithm Development

54 CSC118 Fundamentals of Algorithm Development
Exercise 6e A laundry service charges its customers based on the following scheme: All laundries that are sent for washing are charge RM2.00 per kg of laundry All laundries that are sent for ironing are charge RM3.00 per kg of laundry Write a pseudocode and draw a flowchart for the following modules: Module calcWash() that receives the weight of the laundry sent for washing and returns the charges. Module calcIron() that receives the number of pieces sent for ironing and returns the charges. Module printBill() that receives the name of the customer and the total charges. Then, display the name and the charges. Main module will asked the user to enter name, weight of the laundry sent for washing and number of pieces sent for ironing. The algorithm will prints the bill of the laundry for 10 customers. CSC118 Fundamentals of Algorithm Development

55 In-out Formal Parameter for reference parameter
Problem solving using in-out formal parameter Task: Display the numbers in ascending order. Write a module that receives three numbers and returns the sorted numbers. Write a main module that asked the user to enter three numbers. Call the above module and display the numbers in ascending order. CSC118 Fundamentals of Algorithm Development

56 In-out Formal Parameter (cont.)
Step 1: A basic algorithm is as follows: After statement3, the first number stores the smallest number. CSC118 Fundamentals of Algorithm Development

57 In-out Formal Parameter (cont.)
Step 2: Convert a basic algorithm to a main module CSC118 Fundamentals of Algorithm Development

58 In-out Formal Parameter (cont.)
Step 3: The module Sort() swaps between two numbers and returns the sorted two numbers. CSC118 Fundamentals of Algorithm Development

59 In-out Formal Parameter (cont.)
The module Sort() needs two values, the first number and the second number. These values came from the main module through formal parameters type in formal parameters. Then the swap process occurred to exchange the values. Lastly these swapped values are send back to the main module through out formal parameters. The in formal parameters are named no1 and no2. The out formal parameters are also named no1 and no2. So the no1 and no2 have to be set as type in-out formal parameter. CSC118 Fundamentals of Algorithm Development

60 In-out Formal Parameter (cont.)
Step 4: A complete algorithm: CSC118 Fundamentals of Algorithm Development

61 In-out Formal Parameter (cont.)
The relationship between the main module and the module sort() is as follows: CSC118 Fundamentals of Algorithm Development

62 CSC118 Fundamentals of Algorithm Development

63 Advantages Using Module
Through this approach, it is easier to get the solution. When solution contains any error, it is easy to debug because the debugging process only focuses on one entire module where the error exists. The same module can be used repeatedly at different part in the main module. The solution is more systematic. CSC118 Fundamentals of Algorithm Development

64 CSC118 Fundamentals of Algorithm Development
Thank you… CSC118 Fundamentals of Algorithm Development


Download ppt "Algorithm development"

Similar presentations


Ads by Google