Top-Down Design with Functions

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.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
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.
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.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
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.
Chapter 9: Value-Returning Functions
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Function Topic 4.
Lesson #6 Modular Programming and Functions.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
About the Presentations
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Chapter 8: Introduction to High-Level Language Programming
User Defined Functions
Functions.
Lec8.
Overview of Computers and C Programming
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Introduction to Functions Defining and Calling a Function
Review of C++ Language Basics
CPS125.
Top-Down Design with Functions
Presentation transcript:

Top-Down Design with Functions Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ. http://ant.comm.ccu.edu.tw jypan@comm.ccu.edu.tw

Outline Building a program Top-Down Design and Structure Charts Case Study Finding Area/Circumference of a circle Top-Down Design and Structure Charts Case Study: Drawing Simple Diagrams Code reuse and functions

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)

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.

Case Study Finding the Area and Circumference of a circle(2/4) Step 2: Analysis Problem Constant PI 3.14159 Problem Inputs radius Problem Outputs area circum Relevant Formulas Area of a circle = π x radius2 Circumference of a circle = 2 π x radius

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

Case Study Finding the Area and Circumference of a circle(4/4) Step 4: Implementation (Figure 3.2、Figure 3.3) Step 5: Testing

Outline of Program calculating Circle

Calculating Area and Circumference of a Circle

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

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

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.

Figure 3.10 Structure Chart for Drawing a Stick Figure

Figure 3.11 Function Prototypes and Main Function for Stick Figure

Figure 3.12 Function draw_circle

Figure 3.13 Function draw_triangle

Figure 3.14 Whole Program to Draw a Stick Figure

Figure 3.14 Program to Draw a Stick Figure (cont’d)

Execution order of subprograms and main Figure 3 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 1. When executes function call statement, it transfer control to referenced function. 3. Execute statements in subprogram function 6. Execute next statement in the main function 4. Return control to the main function 5. Release memory

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 (教到字串時請自學) 自行參考其功用,大略記一下 function call y = sqrt(x); Function sqrt as a “Black Box” function name argument

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

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

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 Testing Function scale

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.

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

Data Areas After Call scale(num_1, num_2);

Question? A good question deserve a good grade…