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

Slides:



Advertisements
Similar presentations
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Advertisements

Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman.
Computer Programming w/ Eng. Applications
Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Representation of Data Types Comparing double and int data types Using integers are faster and more precise round-off errors when using doubles The range.
Adapted from Pearson Addison-Wesley. Addison Wesley is an imprint of Chapter 3: Math Functions Adapted from Problem Solving & Program Design in C Sixth.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions Problem Solving and Program Design.
ICS103: Programming in C 3: Top-Down Design with Functions
UNIT 4 Top-Down Design & Functions.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Top-Down Design with Functions and Classes By Dr. Awad Khalil Computer Science & Engineering Department.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 2: Input, Processing, and Output
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
COS120 Software Development Using C++ AUBG Fall semester 2010
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Top-Down Design with Functions and Classes Chapter 3.
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
C++ Programming: Basic Elements of C++.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Top-Down Design SLO1.8 Explain the benefits of dividing large programming tasks to smaller.
Lecture 8: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions and Classes Problem Solving, Abstraction,
chap3 Chapter 3 Top-Down Design with Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Circumference Review. Review What is the relationship between a radius and a diameter? What does a circumference measure? What formulas do we use to calculate.
JavaScript 101 Lesson 3: Variables and Arithmetic.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 6 Section 5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6 Section 5. Objectives 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Solving Quadratic Equations by Factoring Solve quadratic equations.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3 Top-Down Design with Functions. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-2 Figure 3.1 Edited Data Requirements and Algorithm.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS /1433 – Term 1.
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 3: Top-Down Design with Functions and Classes
1) C program development 2) Selection structure
Top-Down Design with Functions
Top-Down Design with Functions
Computer Science II CS132/601* Lecture #C-1.
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-4 Figure 3.1 Edited Data Requirements and Algorithm for Conversion Program

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

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-7 Figure 3.2 Outline of Program Circle

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

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

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 –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-12 Figure 3.4 Computing the Rim Area of a Flat Washer

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-15 Figure 3.5 Flat Washer Program

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

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

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.

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-20 Figure 3.7 Square Root Program

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

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

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

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

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

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.

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