Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 : Algorithm Development

Similar presentations


Presentation on theme: "Chapter 6 : Algorithm Development"— Presentation transcript:

1 Chapter 6 : Algorithm Development

2 Contents 1 2 3 4 Approach : Top Down & Bottom Up Design Stub
Stepwise Refinement 4 Module

3 1. Top Down Design INTRODUCTION
During the 1970s and 80s, the primary software engineering methodology is called structured programming approach. When software engineer design a program, they will try to break down the problem into smaller pieces and work on each smaller piece separately, continues this process until every smaller pieces of problem are easy to work with without further decomposition. We call this designing method 'Top-Down' programming method.

4 …continue (Top Down Design)
Top down design is usually possible only when programmer start by having a very clear idea of the problem and how to solve it. This approach has its value and popular approach at that time. The structured design approach is also known as Top Down Design, Stepwise Refinement and Modular Programming

5 …continue (Top Down Design)
A programming style, enhanced from traditional procedural languages The technique for writing a program using top-down methods is to write a main procedure that names all the major functions it will need. Then looks at the requirements of each of those functions and the process is repeated. interfaces become clearly defined because identify the main level and breaking it into sub level Company Logo

6 …continue (Top Down Design)
If we look at a problem as a whole, it may seem impossible to solve because it is so complex. Examples: writing a tax computation program Complex problems can be solved using top-down design, also known as stepwise refinement, where We break the problem into parts Then break the parts into parts Soon, each of the parts will be easy to do

7 …continue (Top Down Design)
Top Down design is the standard way of writing programs. Programs produced using this method and using only the three kinds of control structures, sequential, selection and repetition, are called structured programs. Structured programs are easier to test, modify, and are also easier for other programmers to understand.

8 Steps in designing Top Down Design
Should any of these steps be broken down further? Possibly. Check whether or not programmer could easily write the algorithm for the step. If not, break it down again. If you are comfortable with the breakdown, write the pseudocode for each of the steps (modules) in the hierarchy. Typically, each module will be coded as a separate function.

9 Advantages of Top-Down Design
Breaking the problem into parts helps us to clarify what needs to be done. At each step of refinement, the new parts become less complicated and, therefore, easier to figure out. 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.

10 …continue (Advantage Top Down Design)
Separating the low level work from the higher level abstractions leads to a modular design. Modular design means development can be self contained. Having "skeleton" code illustrates clearly how low level modules integrate. Fewer operations errors (to reduce errors, because each module has to be processed separately, so programmers get large amount of time for processing).

11 Much less time consuming (each programmer is only involved in a part of the big project).
Very optimized way of processing (each programmer has to apply their own knowledge and experience to their parts (modules), so the project will become an optimized one). Easy to maintain (if an error occurs in the output, it is easy to identify the errors generated from which module of the entire program).

12 Weakness Top Down Design
When one piece of code is develop in previous project, that piece of source code is depend on the upper level of code and unique to each project due to the top-down approach. Need to redevelop some code that have already develop. Therefore, the cost of producing a high-quality program will become very high.

13 An Example of Top-Down Design
We own a home improvement company. We do painting, roofing, and basement waterproofing. A section of town has recently flooded (zip code 21222). We want to send out brochure to our customers in that area. How you want to send the brochure?

14 ANSWER: Get the customer list from a file. Sort the list according to zip code. Make a new file of only the customers with the zip code from the sorted customer list. Print an envelope for each of these customers. Print Read Main Sort Select

15 EXAMPLE 2 Problem: Write a program that draws this picture of a house.

16 Draw the outline of the house
Draw the chimney Draw the door Draw the windows Main Draw Chimney Door Windows Outline

17 Call Draw Outline Call Draw Chimney Call Draw Door Call Draw Windows

18 The door has both a frame and knob. We could break this into two steps.
Main Draw Chimney Door Windows Outline Door Frame Knob

19 Call Draw Door Frame Call Draw Knob

20 Another Observation (Top Down Design)
There are three windows to be drawn. Main Draw Windows Outline Window 2 Window 1 Draw Window 3

21 One Last Observation (Top Down Design)
The windows look the same but have different locations. So, reuse the code that draws a window. Simply copy the code three times and edit it to place the window in the correct location, or Use the code three times, “sending it” the correct location each time

22 Reusing the Window Code (Top Down Design)
This is an example of code reuse. Main Draw Windows Outline Draw a Window

23 Pseudocode to Draw Windows (Top Down Design)
Call Draw a Window, sending in Location 1 Call Draw a Window, sending in Location 2 Call Draw a Window, sending in Location 3

24 EXERCISES EXERCISES Write a program that will calculate employees salary. Use hierarchy chart and pseudocode to explain the details. Company Logo

25 Answer Hierarchy Chart
Validate Input Calculate Salary Print Get Input EmpNo EmpName PayRate HoursWork Salary and Overtime Main

26 Answer Pseudocode Call Get Input Call Validate Input
Call Calculate Salary Calculate Salary Calculate Salary and Bonus Call Print Salary

27 2. Bottom up Design INTRODUCTION
Start the design process at the bottom. Opposite the Top Down approach Idea of the problem have been identified, or already have source code which can fit into the project

28 2. Bottom up Design INTRODUCTION
Object-oriented programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. is common in object-oriented languages such as C++ or Java. usually use together with Top-down approach to solve problems. Modern software design approaches usually combine Bottom up and Top Down approach Company Logo

29 …continue (Bottom up Design)
The individual base elements of the system are first specified in great detail. These elements are then linked together: to form larger subsystems are linked in many levels until a complete top-level system is formed. Often resembles a "seed" model the beginnings are small but eventually grow in complexity and completeness.

30 …continue (Bottom up Design)
Bottom-up emphasizes coding and early testing, which can begin as soon as the first module has been specified. Has the risk that modules may be coded without having a clear idea of how they link to other parts of the system and that such linking may not be as easy as first thought.

31 …continue (Bottom Up Design)
Lego Racer Pro/ENGINEER Parts is a good example of bottom-up design The parts are first created and then assembled without regard to how the parts will work in the assembly.

32 Weakness & Advantage of Bottom Up Design
Bottom-up advantage If a system is to be built from existing system, this approach is more suitable as it starts from some existing modules. Bottom-up weakness Need to use a lot of intuition to decide the functionality that is to be provided by the module.

33 Top Down vs Bottom Up

34 STUB A small software routine placed into a program that provides a common function. Stubs are used for a variety of purposes. A stub is a small program routine that substitutes for a longer program, possibly to be loaded later or that is located remotely. It can also be used as testing or before the program are fully developed.

35 Example STUB Create a program that draws a diamond or a triangle with a size that the user selects using a specific character that will be entered at the keyboard.  Here are two examples: A triangle of size 4, using *: * *** ***** ******* A diamond of size 4, @ What do we need for this program?

36 …continue (Example STUB)
Problem definition Define the problem, the inputs, and the outputs Draw either a diamond or a triangle of the requested size using the specified character. Inputs: A choice to draw one of the two shapes or to quit the program, A character choice which will be used for drawing the selected shape, and The size of the shape Output: The output will be one of the following: A triangle, A diamond, Quit Analysis of the problem Break down the tasks that this program is supposed to complete. The main tasks are: Ask for the input data For both the triangle and the diamond, display a triangle of the requested size If the choice is a diamond, display the bottom part

37 ...continue(STUB) The Algorithm Design Function main():
Display the instructions Display the menu Store user's choice Check if it's a valid choice Call draw_shape(choice) End //draw_shape(choice) draws either a triangle or a diamond Function draw_shape(choice): Get size of the shape Get fill character for shape If choice = triangle Call draw_triangle(size,character) Else choice = diamond Call draw_diamond(size,character)

38 STEPWISE REFINEMENT Representation of some required program is refined through a sequence of intermediate representations to get a final program Subsequent development then proceeds in a sequence of small steps. Each step refines some aspect of the representation produced by the previous step, thus yielding the next representation in the sequence. Typically a single step involves simultaneous refinement of both data structures and operations, and is small enough to be performed with some confidence that the result is correct.

39 STEPWISE REFINEMENT Break a complex problem down into a number of simpler steps each of which can be solved by an algorithm which is smaller and simpler than the one required to solve the overall problem. easier to construct and sketch the detail Sub-algorithms can themselves be broken into smaller portions Refinement of the algorithm continues in this manner until each step is sufficiently detailed. Refinement means replacing existing steps/instructions with a new version that fills in more details

40 STEPWISE REFINEMENT FIRST REFINEMENT ORIGINAL ALGORITHM
1. Put tea leaves in pot Boil Water Add water to pot Wait 5 Minutes Pour tea into cup ORIGINAL ALGORITHM 1.1 Open box of tea Extract one spoonful Tip spoonful into pot Close box of tea Fill kettle with water Switch on kettle Wait until water boiled Switch off kettle 3.1 Pour water from kettle until pot is full Pour tea from pot into cup until cup is full FIRST REFINEMENT 1.1.1 Take tea box from shelf Remove lid from box Put lid on box Replace tea box on shelf Put kettle under tap Turn on tap Wait until kettle is full Turn off tap Wait until kettle whistles SECOND REFINEMENT 4040

41 Module / Modular Concept
Must be large enough to perform its task and include only operations that contribute to the performance of the task Modularization is the process of dividing a problem into separate tasks, each with a single purpose In C++ programming, modules are called Functions

42 Module / Modular Concept
Company Logo

43 Module / Modular Concept
Example double   squared (double);        void   print_report (int);              double  squared(double number)    {                                                                  return (number * number);         }                                                          void    print_report(int report_number) {        if(report_number  == 1)               printf("Printing Report 1");        else               printf("Not printing Report 1"); } Company Logo

44 Modular Design Benefits of modular design: Ease of Understanding
Each module should perform just one function Eliminate of redundancy Avoid the repetition of writing out the same code more than once Efficiency of maintenance Each module should be self-contained and have little or no effect on other modules within the algorithm Company Logo

45 Example Example : Design a solution algorithm that allow users input three number input. Program will sort the input into ascending sequence and display the output. The program will end when user type “XXX”. Company Logo

46 Example Problem Analysis: Input : Process: Output: Num1, Num2, Num3
Accept three numbers Sort the numbers Output: Display the numbers in ascending orders Company Logo

47 Answer Hierarchy Chart
Main Get Input Accept Numbers Sort Numbers Display Numbers Exit “XXX”

48 Parameter Passing Parameters are data items transferred from a calling module to its subordinate module at the time of calling. When the subordinate module terminates and returns control to its caller, the values in the parameters are transferred back to the calling module Therefore, the only interaction between a module and the rest of the program is through parameters Company Logo

49 …continue (Parameter Passing)
Example : Parameters can use different names but the type and order must be the same Main Call Print_Page_Headings (a, b) …. ….. Function Print_Page_Headings (y, z) Call Module Print_page_Headings a, b are list of parameters to be passed CALLING MODULE SUBORDINATE MODULE Module Print_page_Headings being called y, z are list of parameters to be received from the calling module

50 …continue (Parameter Passing)
Parameters may have one of three functions: To pass information from a calling module to a subordinate module for processing To pass information from a subordinate module to its calling module. The calling module would then use the parameter in subsequent processing To fulfill a two way communication role. The calling module may pass information to a subordinate module for processing then return back to the calling module Company Logo

51 …continue (Parameter Passing)
Two types of parameter: Value parameters Only pass data one way which is the value of the parameter The value returns to its original value Reference parameters Pass data to a called module where that data may be changed and then passed back to the calling module Use reference address of the parameter The value of the parameter may be referenced and changed during the processing of the sub module Company Logo

52 Thank You !


Download ppt "Chapter 6 : Algorithm Development"

Similar presentations


Ads by Google