Chapter 6 : Algorithm Development

Slides:



Advertisements
Similar presentations
Working With Algorithm and Flowcharts
Advertisements

CMSC 104, Version 9/011 Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.9, 3.10.
Chapter 1 An Overview of Computers and Programming Languages.
Programming Logic and Design Fourth Edition, Introductory
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 1 Program Design
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Algorithms IV: Top-Down Design
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Developing a Program.
Simple Program Design Third Edition A Step-by-Step Approach
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
CS001 Introduction to Programming Day 5 Sujana Jyothi
Top-Down Design and Modular Development
Program Design CMSC 201. Motivation We’ve talked a lot about certain ‘good habits’ we’d like you guys to get in while writing code. There are two main.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
First Steps in Modularization Simple Program Design Third Edition A Step-by-Step Approach 8.
SE: CHAPTER 7 Writing The Program
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CMSC 104, Version 8/061L17Top-DownDesign.ppt Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.1.
CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Chapter 8 First steps in modularisation. Objectives To introduce modularisation as a means of dividing a problem into subtasks To present hierarchy charts.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
L061 Algorithms, Part 3 of 3 Topics Top down-design Structure charts Reading Sections , 3.3.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Introduction to OOP CPS235: Introduction.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING. Computer Programming Concepts. Flowchart. Structured Programming Design. Implementation Documentation.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Algorithms and Flowcharts
MANAGEMENT INFORMATION SYSTEM
Further Modularization, Cohesion, and Coupling. Simple Program Design, Fourth Edition Chapter 9 2 Objectives In this chapter you will be able to: Further.
Advanced Higher Computing Science
IL Marking Get out your CPU / Memory answers Swap with someone else
Programming Logic and Design Seventh Edition
Algorithms and Problem Solving
Visit for more Learning Resources
Basics of Computer Programming
Algorithms IV Top-Down Design.
Principles of Programming and Software Engineering
An Introduction to Programming and VB.NET
Algorithm development
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Programming Fundamentals Lecture #7 Functions
CMSC201 Computer Science I for Majors Lecture 11 – Program Design
Basics of Computer Programming
Basics of Computer Programming
Basics of Computer Programming
Chapter 5 - Functions Outline 5.1 Introduction
Designing and Debugging Batch and Interactive COBOL Programs
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 1 Introduction(1.1)
UMBC CMSC 104 – Section 01, Fall 2016
Algorithms IV Top-Down Design.
Algorithms and Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Week 4 Lecture-2 Chapter 6 (Methods).
Paper by D.L Parnas And D.P.Siewiorek Prepared by Xi Chen May 16,2003
Programming Fundamentals (750113) Ch1. Problem Solving
Algorithms, Part 3 of 3 Topics Top down-design Structure charts
Top-Down Design & JSP Skill Area Part D
Algorithms, Part 3 of 3 Topics Top down-design Reading
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Presentation transcript:

Chapter 6 : Algorithm Development

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

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.

…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

…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

…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

…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.

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.

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.

…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).

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).

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.

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?

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 21222 from the sorted customer list. Print an envelope for each of these customers. Print Read Main Sort Select

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

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

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

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

Call Draw Door Frame Call Draw Knob

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

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

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

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

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

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

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

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

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

…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.

…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.

…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.

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.

Top Down vs Bottom Up

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.

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, using @: @ @@@ @@@@@ @@@@@@@ What do we need for this program?

…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

...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)

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.

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

STEPWISE REFINEMENT FIRST REFINEMENT ORIGINAL ALGORITHM 1. Put tea leaves in pot 2. Boil Water 3. Add water to pot 4. Wait 5 Minutes 5. Pour tea into cup ORIGINAL ALGORITHM 1.1 Open box of tea 1.2 Extract one spoonful 1.3 Tip spoonful into pot 1.4 Close box of tea 2.1 Fill kettle with water 2.2 Switch on kettle 2.3 Wait until water boiled 2.4 Switch off kettle 3.1 Pour water from kettle until pot is full 5.1 Pour tea from pot into cup until cup is full FIRST REFINEMENT 1.1.1 Take tea box from shelf 1.1.2 Remove lid from box 1.4.1 Put lid on box 1.4.2 Replace tea box on shelf 2.1.1 Put kettle under tap 2.1.2 Turn on tap 2.1.3 Wait until kettle is full 2.1.4 Turn off tap 2.3.1 Wait until kettle whistles SECOND REFINEMENT 4040

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

Module / Modular Concept Company Logo

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

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

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

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

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

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

…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

…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

…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

Thank You !