Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6: Modular Programming Problem Solving & Program Design in.
Advertisements

Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
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.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
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 6 Modular Programming 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.
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to C Programming
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
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.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
Review 1 List Data Structure List operations List Implementation Array Linked List.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Lecture 17: Modular Programming (cont) Debugging and Debuggers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CISC105 – General Computer Science Class 2 – 6/7/2006.
The Hashemite University Computer Engineering Department
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Modular Programming Problem Solving and Program Design in C 5th.
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
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.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
Bill Tucker Austin Community College COSC 1315
Chapter 7: User-Defined Functions II
Completing the Problem-Solving Process
Chapter 6: Modular Programming
Chapter 10: Void Functions
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
User-Defined Functions
INPUT & OUTPUT scanf & printf.
Simple Data Types and Function Calls
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Chapter 6 Modular Programming chap6.
ICS103: Programming in C 6: Pointers and Modular Programming
Top-Down Design with Functions
Presentation transcript:

Chapter 6 Modular Programming 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.6-2 Functions with Simple Output Parameters 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.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-3 Figure 6.1 Function separate()

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-4 Figure 6.2 Diagram of Function separate() with Multiple Results

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-5 Functions with Simple Output Parameters (Cont’d) Asterisks –before the parameter names –in front of the parameter names in the assignment statements char *signp tells the compiler that signp will contain the address of a type char variable. pointer –a memory cell whose content is the address of another memory cell

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-6 Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-7 Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-8 Functions with Simple Output Parameters (Cont’d) The calling function must declare variables in which function separate can store the multiple results it computes. Changing of the values of memory cells in the data area of the calling function is considered a side effect of the call to function separate. The addresses of the arguments sn, whl, and fr are stored in the corresponding output parameters signp, wholep, and fracp.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-9 Figure 6.4 Parameter Correspondence for separate(value, &sn, &whl, &fr);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-10 Functions with Simple Output Parameters (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-11 Figure 6.5 Comparison of Direct and Indirect Reference

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-12 Functions with Simple Output Parameters (Cont’d) The formal parameter is preceded by the indirection operator, unary *. –*signp = '+'; –*wholep = floor(magnitude); –*fracp = magnitude - *wholep;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-13 Meanings of * Symbol Three distinct meanings of the symbol * –the binary operator meaning multiplication. –in the declarations, read as “pointer to” –the unary indirection operator, “follow the pointer”

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-14 Figure 6.6 Program to Sort Three Numbers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-15 Figure 6.6 Program to Sort Three Numbers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-16 Multiple Calls to a Function with Input/Output Parameters sort –a rearrangement of data in a particular sequence (increasing or decreasing) input/output parameters –the function uses the current actual argument values as inputs and may return new values

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-17 Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-18

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-19 Scope of Names scope of a name –the region in a program where a particular meaning of a name is visible All of the formal parameters and local variables are visible only from their declaration to the closing brace in which they are declared.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-20 Figure 6.8 Outline of Program for Studying Scope of Names

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-21

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-22 Figure 6.9 Function scan_fraction (incomplete)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-23 Figure 6.9 Function scan_fraction (incomplete) (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-24 Figure 6.10 Data Areas for scan_fraction and Its Caller

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-25 CASE STUDY Arithmetic with Common Fractions To perform computations with common fractions and get results that are common fractions in reduced form Add, subtract, multiply, and divide several pairs of common fractions.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-26 CASE STUDY Arithmetic with Common Fractions (Cont’d) DATA REQUIREMENTS –Problem Inputs int n1, d1 /* numerator, denominator of first fraction */ int n2, d2 /* numerator, denominator of second fraction */ char op /* arithmetic operator + - * or / */ char again /* y or n depending on user's desire to continue */ –Problem Outputs int n_ans /* numerator of answer */ int d_ans /* denominator of answer */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-27 CASE STUDY Arithmetic with Common Fractions (Cont’d) INITIAL ALGORITHM –1. Repeat as long 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. Step 2 Refinement –2.1 Get first fraction. –2.2 Get operator. –2.3 Get second fraction.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-28 CASE STUDY Arithmetic with Common Fractions (Cont’d) Step 3 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. Step 3.2 Refinement –3.2.1 Find the greatest common divisor (GCD) of the numerator and denominator. –3.2.2 Divide the numerator and denominator by the GCD.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-29 Figure 6.11 Structure Chart for Common Fraction Problem

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-30 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-31 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-32 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-33 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-34 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-35 Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-36 Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-37 Debugging and Testing a Program System Not all functions will be ready at the same time use of stubs enables us to test and debug the main program flow and those functions that are available top-down testing –the process of testing flow of control between a main function and its subordinate functions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-38 Debugging and Testing a Program System (Cont’d) 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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-39 Figure 6.14 Stub for Function multiply_fractions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-40 Debugging and Testing a Program System (Cont’d) unit test –a test of an individual function –Perform a preliminary test of a new function –It is easier to locate and correct errors when dealing with a single function rather than with a complete program system. Perform a unit test by writing a short driver function to call it. Once a function works properly, substitute it for its stub

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-41 Figure 6.15 Driver for Function scan_fraction

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-42 Debugging and Testing a Program System (Cont’d) bottom-up testing –the process of separately testing individual functions of a program system system integration tests –testing a system after replacing all its stubs with functions that have been pretested

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-43 Debugging Tips for Program Systems A list of suggestions for debugging a program system follows. –Carefully document each function parameter and local variable using comments –Create a trace of execution by displaying the function name as you enter it –Trace or display the values of parameters upon entry to a function –Trace or display the values of all function outputs

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-44 Common Programming Errors An actual output argument must be of the same pointer data type as the corresponding formal parameter. The C scope rules