# Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.

## Presentation on theme: "Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National."— Presentation transcript:

Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISTING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF A CIRCLE –CASE STUDY: FINDING THE WEIGHT OF A BATCH OF FLAT WASHERS 3.2 LIBRARY FUNCTIONS 3.3 TOP-DOWN DESIGN AND STRUCTURE CHARTS –CASE STUDY: DRAWING SIMPLE DIAGRAMS 3.4 FUNCTIONS WITHOUT ARGUMENTS 3.5 FUNCTIONS WITH INPUT ARGUMENTS 3.6 COMMON PROGRAMMING ERRORS CHAPTER REVIEW

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-3 Building Programs from Existing Information The solution can be developed from information that already exists or from the solution to another problem. You can use documentation as a starting point in coding your program: –Edit the data requirements to conform to the C syntax for constant macro definitions and variable declarations –Use the initial algorithm and its refinements as program comments –After the comments are in place in the main function, you can begin to write the C statements

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-5 CASE STUDY Finding the Area and Circumference of a Circle PROBLEM –Get the radius of a circle. Compute and display the circle’s area and circumference. ANALYSIS –Problem Constant PI 3.14159 –Problem Input radius /* radius of a circle */ –Problem Outputs area /* area of a circle */ circum /* circumference of a circle */ –Relevant Formulas area of a circle: π radius 2 circumference of a circle: 2πradius

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-6 CASE STUDY Finding the Area and Circumference of a Circle (Cont’) DESIGN –INITIAL ALGORITHM 1.Get the circle radius. 2.Calculate the area. 3.Calculate the circumference. 4.Display the area and the circumference. –ALGORITHM REFINEMENTS 2.1 Assign PI * radius * radius to area. 3.1 Assign 2 * PI * radius to circum. –IMPLEMENTATION Outline Coding

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-10 CASE STUDY Computing the Weight of a Batch of Flat Washers PROBLEM –You work for a hardware company that manufactures flat washers. To estimate shipping costs, your company needs a program that computes the weight of a specified quantity of flat washers.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-11 Analysis for Computing the Weight of a Batch of Flat Washers ANALYSIS –To compute the weight of a single flat, washer, you need to know its rim area, its thickness, and the density. –The rim area must be computed from two measurements that are provided as inputs: the washer’s outer diameter and its inner diameter –Problem Constant PI 3.14159 –Problem Inputs double hole_diameter /* diameter of hole */ double edge_diameter /* diameter of outer edge */ double thickness /* thickness of washer */ double density /* density of material used */ double quantity /* number of washers made */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-13 CASE STUDY Computing the Weight of a Batch of Flat Washers (Cont’) –Problem Outputs double weight /* weight of batch of washers */ –Program Variables double hole_radius /* radius of hole */ double edge_radius /* radius of outer edge */ double rim_area /* area of rim */ double unit_weight /* weight of 1 washer */ –Relevant Formulas area of a circle: πradius 2 radius of a circle: diameter / 2 rim area: area of outer circle - area of hole unit weight: rim area * thickness * density

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-14 CASE STUDY Computing the Weight of a Batch of Flat Washers (Cont’) DESIGN –INITIAL ALGORITHM 1.Get the washer’s inner diameter, outer diameter, and thickness 2.Get the material density and quantity of washers manufactured 3.Compute the rim area 4.Compute the weight of one flat washer 5.Compute the weight of the batch of washers 6.Display the weight of the batch of washers –ALGORITHM REFINEMENTS 3.1 Compute hole_radius and edge_radius. 3.2 rim_area is PI * edge_radius * edge_radius - PI * hole_radius * hole_radius 4.1 unit_weight is rim_area * thickness * density –IMPLEMENTATION

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-18 Library Functions -- Predefined Functions and Code Reuse (Cont’) If x is 16.0, the assignment statement above is evaluated as follows: 1.x is 16.0, so function sqrt computes the, or 4.0. 2.The function result, 4.0, is assigned to y. z = 5.7 + sqrt(w); 1.If w is 9.0, function sqrt computes the square root of 9.0, or 3.0. 2.The values 5.7 and 3.0 are added together. 3.The sum, 8.7, is stored in z.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-19 a function argument can also be an expression –first_sqrt = sqrt(first); second_sqrt = sqrt(second); sum_sqrt = sqrt(first + second); Figure 3.6 Function sqrt as a “Black Box”

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-23 C Library Functions (Cont’d) If one of the functions in Table 3.1 is called with a numeric argument that is not of the argument type listed, the argument value is converted to the required type before it is used. –Conversions of type int to type double cause no problems. –A conversion of type double to type int leads to the loss of any fractional part.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-24 C Library Functions (Cont’) Compute the roots of a quadratic equation in x of the form ax 2 + bx + c = 0. –The two roots are defined as: –C Code: /* Compute two roots, root_1 and root_2, for disc > 0.0 */ disc = pow(b,2) - 4 * a * c; root_1 = (-b + sqrt(disc)) / (2 * a); root_2 = (-b - sqrt(disc)) / (2 * a);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-25 C Library Functions (Cont’) a 2 = b 2 + c 2 - 2bc cos α a = sqrt(pow(b,2) + pow(c,2) - 2 * b * c * cos(alpha * PI / 180.0));

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-27 Top-Down Design and Structure Charts top-down design –a problem-solving method in which you first break a problem up into its major subproblems and then solve the subproblems to derive the solution to the original problem structure chart –a documentation tool that shows the relationships among the subproblems of a problem

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-28 CASE STUDY Drawing Simple Diagrams PROBLEM –You want to draw some simple diagrams on your printer or screen. Two examples are the house and female stick figure in Fig. 3.9. ANALYSIS –We can draw both figures with these four basic components: a circle a base line parallel lines intersecting lines