Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6: Modular Programming Problem Solving & Program Design in.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Introduction to C Programming
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 5 Repetition and Loop Statements 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.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of.
CS 201 Functions Debzani Deb.
Review Ch 1~2 Overview of Computers and C Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to C Programming
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Modular Programming Advantages of using functions (to make a modular program) are: Changing the program into separate pieces Code reusing Easier modification.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Lecture 17: Modular Programming (cont) Debugging and Debuggers.
1 ICS103 Programming in C Lecture 8: Functions I.
CISC105 – General Computer Science Class 2 – 6/7/2006.
The Hashemite University Computer Engineering Department
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Modular Programming Problem Solving and Program Design in C 5th.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: User-Defined Functions II
Completing the Problem-Solving Process
Chapter 6: Modular Programming
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
The Selection Structure
User-Defined Functions
Basic Elements of C++ Chapter 2.
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Chapter 6 Modular Programming chap6.
Top-Down Design with Functions
ICS103: Programming in C 6: Pointers and Modular Programming
Top-Down Design with Functions
Presentation transcript:

Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-2 本章重點  Pointer  Name scope  Testing skill

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Function With Output Parameter  Argument lists provide the communication links between the main function and its function subprograms.  Arguments enable a function to manipulate different data each time it is called.  When a function call executes, the computer allocates memory space in the function data area for each formal parameter. The value of each actual parameter is stored in the memory cell allocated to its corresponding formal parameter.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-4 Figure 6.1 Function separate

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-5 Pointer A memory cell whose content is the address of another memory cell Fig 1.4 Fig 3.15 Actual & Formal

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-6  If a reference x is of type “whatever-type”, the reference &x is of type “pointer to whatever-type,” that is, “whatever-type *.”

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-7 Figure 6.5 Comparison of Direct and Indirect Reference

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Meaning of * Symbol  Three distinct meanings of the symbol *  Multiplication  3 * x + 4 (expression)  Part of the names of the parameters’ data types  pointer to  char *signp; (declaration)  Unary indirection operator  follow the pointer  *signp = ‘+’; (execution statement)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Multiple Calls to a Function with Input/Output Parameters  sort  A rearrangement of data in a particular sequence (increasing or decreasing)  Example 6.2  The main function in Fig. 6.6 gets three data values and rearranges the data in increasing sequence.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-10 Figure 6.6 Program to Sort Three Numbers

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-11 Figure 6.6 Program to Sort Three Numbers (cont’d) Comparing to p.164, Fig 4.5

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Trace of Program to Sort Three Numbers Statementnum1num2num3Effect scanf(“...”, &num1, &num2, &num3); Enter data Order(&num1, &num2);No change Order(&num1, &num3); Switch Order(&num2, &num3); Switch printf(“...”, num1, num2, num3) Display

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-13 Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-14 Program Style  Functions that return a single value are the easiest functions for a program reader to deal with.  If a function subprogram has a meaningful name, the reader can often get a good idea of what is happening in the calling function.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Scope of Names  The scope of a name refers to the region of a program where a particular meaning of a name is visible or can be referenced.  Name is defined as constant macros and their scope begins at their definition and continues to the end of the source file.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-16 Figure 6.8 Outline of Program for Studying Scope of Names

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-17 Scope of Names Use in Fig. 6.8

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Formal Output Parameters as Actual Arguments  Fig. 6.9 shows a function that scans a data line representing a common fraction of the form  numerator / denominator  Numerator is an integer  Denominator is a positive integer  Figure 6.10 shows the data areas for scan_fraction and the function calling it.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-19 Figure 6.9 Function scan_fraction nump &slash denomp Driver: Fig 6.15

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-20 Figure 6.10 Data Areas for scan_fraction and Its Caller scanf()

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab A Program with Multiple Functions CASE STUDY Arithmetic with Common Fractions

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-22

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Case Study: Arithmetic with Common Fractions  Design  Initial Algorithm  1.Repeat as log as user wants to continue  2. Get a fraction problem  3. Compute the result  4. Display problem and result  5. Check if user wants to continue  Refinement  2.1 Get first fraction  2.2 Get operator  2.3 Get second fraction

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Case Study: Arithmetic with Common Fractions  Design  Refinement  3.1 Select a task based on operator  Add the fractions  Add the first fraction and the negation of the second  Multiply the fractions  Multiply the first fraction and the reciprocal of the second  3.2 Put the result fraction in reduced form

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Case Study: Arithmetic with Common Fractions  Design  Refinement of 3.2  Find the greatest common divisor (gcd) of the numerator and denominator  Divide the numerator and denominator by the gcd  Fig shows the data flow among the steps

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-26 Figure 6.11 Structure Chart for Common Fraction Problem Fig 6.9 HW5

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Case Study: Arithmetic with Common Fractions  Implementation (Figure 6.12)  stub  A skeleton function that consists of a header and statements that display trace messages and assign values to output parameters  Enables testing of the flow of control among functions before this function is completed  multiply_fractions and find_gcd use stubs

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-28 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions 直接看程式 …

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-29 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-30 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-31 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-32 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-33 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-34 Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-35  Testing (Figure 6.13)  Insert a stub for each function not yet completed  Each stub prints an identification message and assigns values to its output parameters

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Debugging and Testing a Program System  The number of statements in a program system grows, the possibility of error also increases.  keep each function to a manageable size  Top-down testing  the process of testing flow of control between a main function and its subordinate functions  Bottom-up testing  The process of separately testing individual functions of a program system  Unit test  A test of an individual function  System integration tests  Testing a system after replacing all its stubs with functions that have been pretested

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-37 Figure 6.15 Driver for Function scan_fraction  Driver  A short function written to test another function by defining its arguments, calling it, and displaying its result.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab (cont) Debugging Tips for Program Systems  Carefully document each function parameter and local variable using comments.  Create a trace of execution by displaying names  Trace or display the values of all input and input/output parameters  Trace or display the values of all function outputs after returning from a function  Make sure that a function stub assigns a value to the variable pointed to by each output parameter  Use “step into” and “step over” in debugger

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Common Programming Errors  N O T  Beware of name scope and life time  Is there any identifier referenced outside its scope?  Does the identifier refer to what you want to?  …