Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 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

2 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

3 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

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-4 Figure 3.1 Edited Data Requirements and Algorithm for Conversion Program

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

6 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

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-7 Figure 3.2 Outline of Program Circle

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-8 Figure 3.3 Calculating the Area and the Circumference of a Circle

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-9 Figure 3.3 Calculating the Area and the Circumference of a Circle (cont’d)

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

11 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 –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 */

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-12 Figure 3.4 Computing the Rim Area of a Flat Washer

13 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

14 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

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-15 Figure 3.5 Flat Washer Program

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-16 Figure 3.5 Flat Washer Program (cont’d)

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-17 Library Functions -- Predefined Functions and Code Reuse Code reuse Predefined functions –C’s standard math library

18 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 The function result, 4.0, is assigned to y. z = sqrt(w); 1.If w is 9.0, function sqrt computes the square root of 9.0, or The values 5.7 and 3.0 are added together. 3.The sum, 8.7, is stored in z.

19 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”

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-20 Figure 3.7 Square Root Program

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-21 Figure 3.7 Square Root Program (cont’d)

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-22 C Library Functions

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

24 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);

25 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));

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-26 C Library Functions (Cont’) Assume that we have already written functions find_area and find_circum: –Function find_area(r) returns the area of a circle with radius r. –Function find_circum(r) returns the circumference of a circle with radius r. –Fig. 3.3 can be revised as:Fig. 3.3 area = find_area(radius); circum = find_circum(radius); –Fig. 3.5 can be revised as:Fig. 3.5 rim_area = find_area(edge_radius) - find_area(hole_radius);

27 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

28 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 ANALYSIS –We can draw both figures with these four basic components: a circle a base line parallel lines intersecting lines

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-29 Figure 3.9 House and Stick Figure

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-30 CASE STUDY Drawing Simple Diagrams (Cont’) DESIGN –INITIAL ALGORITHM (for the female figure) 1.Draw a circle. 2.Draw a triangle. 3.Draw intersecting lines. –ALGORITHM REFINEMENTS 2.1 Draw intersecting lines. 2.2 Draw a base. –You can use a structure chart to show the relationship between the original problem and its subproblems. –The algorithm, not the structure chart, shows the order in which you carry out each step to solve the problem.

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-31 Figure 3.10 Structure Chart for Drawing a Stick Figure


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

Similar presentations


Ads by Google