Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

Slides:



Advertisements
Similar presentations
Chapter 3: Top-Down Design with Functions Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman.
Advertisements

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.
Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
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.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Common Programming Errors Syntax Errors (Figure 2.15) –missing semicolon at the end of the variable.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Chapter 2: Input, Processing, and Output
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
ICS103 Programming in C Lecture 9: Functions I
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.
Review Ch 1~2 Overview of Computers and C Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
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.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
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.
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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
chap3 Chapter 3 Top-Down Design with Functions.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
CISC105 – General Computer Science Class 2 – 6/7/2006.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
1 ICS103 Programming in C Lecture 9: Functions I.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Function Topic 4.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
User-Defined Functions
Lec8.
Overview of Computers and C Programming
Chapter 6: User-Defined Functions I
Topics Introduction to Functions Defining and Calling a Function
Top-Down Design with Functions
Top-Down Design with Functions
Presentation transcript:

Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-2 Outline Building a program –Case Study Finding Area/Circumference of a circle Top-Down Design and Structure Charts –Case Study: Drawing Simple Diagrams Code reuse and functions

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-3 Building Programs from Existing Information (Design  Implementation) Programs = Data structures + Algorithms How to implement a program? You can… –editing data requirements conform to C syntax for constant macro and variable declarations (Data structures) –use the initial algorithm and refinements as program comments, and then write C statements for them (Algorithms)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-4 Case Study Finding the Area and Circumference of a circle(1/4) Step 1: Problem –Get the radius of a circle. Compute and display the circle’s area and circumference.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-5 Case Study Finding the Area and Circumference of a circle(2/4) Step 2: Analysis –Problem Constant PI –Problem Inputs radius –Problem Outputs area circum –Relevant Formulas Area of a circle = π x radius 2 Circumference of a circle = 2 π x radius

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-6 Case Study Finding the Area and Circumference of a circle(3/4) Step 3: Design –Initial Algorithm 1. Get the circle radius 2. Calculate the area 3. Calculate the circumference 4. Display the area and the circumference –Algorithm Refinement 2.1 Assign PI * radius * radius to area 3.1 Assign 2 * PI * radius to circum

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-7 Case Study Finding the Area and Circumference of a circle(4/4) Step 4: Implementation (Figure 3.2 、 Figure 3.3) Step 5: Testing

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-8 Outline of Program calculating Circle

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-9 Calculating Area and Circumference of a Circle

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-10 Top-Down Design and Structure Charts When a problem is more complex than those we have seen so far… –Must break up the problem into subproblems to develop the program solution, and so on… –Top-down design –A small problem is easier to solve A picture is worth a thousand words –Structure chart –a documentation tool that shows the relationship among the subproblems of a problem

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-11 Case Study: Drawing Simple Diagrams(1/2) Step 1: Problem –You want to draw some simple diagrams on your printer or screen. For example, a female stick figure in Fig.3.9. Step 2: Analysis –three basic components a circle a base line Intersecting lines

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-12 Case Study: Drawing Simple Diagrams(2/2) Step 3: Design (Figure 3.10) –Initial algorithm 1. Draw a circle. 2. Draw a triangle. 3. Draw intersecting lines. –Refinements 2.1 Draw intersecting lines. 2.2 Draw a base.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-13 Figure 3.10 Structure Chart for Drawing a Stick Figure

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-14 Figure 3.11 Function Prototypes and Main Function for Stick Figure

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-15 Figure 3.12 Function draw_circle

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-16 Figure 3.13 Function draw_triangle

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-17 Figure 3.14 Whole Program to Draw a Stick Figure

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-18 Figure 3.14 Program to Draw a Stick Figure (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-19 Execution order of subprograms and main Figure 3.15 Flow of Control Between the main Function and a Function Subprogram 2. Allocate memory for variables declared 5. Release memory 1. When executes function call statement, it transfer control to referenced function. 3. Execute statements in subprogram function 4. Return control to the main function6. Execute next statement in the main function

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-20 Code reuse and functions Why reinvent the wheel? C provides predefined functions –Standard library Appendix B shows ANSI C standard libraries –P.800~801, stdio.h 所列之 functions –P.799, math.h 所列之 functions –P.802, string.h 所列之 functions ( 教到字串時請自學 ) – 自行參考其功用,大略記一下 y = sqrt(x); function call function nameargument Function sqrt as a “Black Box”

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-21 Advantages of Using Function Subprograms Easier to apportion( 分配 ) programming tasks in a team Procedure abstraction –a programming technique in which a main function consists of a sequence of function calls and each function is implemented separately –Defer implementation details (How to do) from a subproblem (What to do). –Focusing on one function at a time is much easier Reuse of function subprograms –Functions can be executed more than once –Functions can be used in other programs or functions

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-22 Functions with Input/output Arguments Lego blocks “protrusions” and “cups” Input arguments –Arguments used to pass information into a function subprogram Output arguments –Arguments used to return results to the calling function

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-23 Testing Function scale Actual argument an expression used inside the parentheses of a function call Formal parameter an identifier that represents a corresponding actual argument in a function definition

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-24 Argument List Correspondence (The not rules) The number of actual arguments used in a call to a function must be the same as the number of formal parameters listed in the function prototype. The order of arguments in the lists determines correspondence. Each actual argument must be of a data type that can be assigned to the corresponding formal parameter.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-25 The Function Data Area When a function call is executed… –a memory area is allocated for storage of function data (ex. formal parameter, local var) When function terminates… –function data area is always lost Again? Local variable –initially undefined (usually…)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-26 Data Areas After Call scale(num_1, num_2);

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-27 Question? A good question deserve a good grade…