Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.

Slides:



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

C Language.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
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.
Overview creating your own functions calling your own functions.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Modular Programming Advantages of using functions (to make a modular program) are: Changing the program into separate pieces Code reusing Easier modification.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Decision Making in Programs.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CPS120: Introduction to Computer Science Functions.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 ICS103 Programming in C Lecture 8: Functions I.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
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.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
User-Defined Functions (cont’d) - Reference Parameters.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Chapter 7: User-Defined Functions II
Chapter 6: Modular Programming
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
User-Defined Functions
7 Arrays.
Simple Data Types and Function Calls
Programming Funamental slides
A First Book of ANSI C Fourth Edition
Programming Funamental slides
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Chapter 6 Modular Programming chap6.
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin

Pointers float *p; – p as a pointer variable of type “pointer to float” int m = 25 ; int *itemp; itemp = &m; Dr. Chow-Sing LinModular Programming - CH 62

Indirect Reference When the unary indirection operator * is applied to a pointer variable, it has the effect of following the pointer referenced by its operand. – Indirect reference Example 6.2 *itemp = 35; Printf(“%d”, *itemp); *itemp = 2 * (*itemp); Dr. Chow-Sing LinModular Programming - CH 63

Pointers to Files Declare that file pointer variable inp and outp will hold information allowing access to the program’s input and output files. FILE *inp; FILE *outp; Open a file inp = fopen(“distance.txt”,”r”); outp = fopen(“distout.txt”, “w”); Dr. Chow-Sing LinModular Programming - CH 64

Pointers to Files (cont.) Read / write data from/to a file fscanf(inp, “%lf”, &item); fprintf(outp, “%.2f\n”, item); Close a file fclose(inp); fclose(outp); Dr. Chow-Sing LinModular Programming - CH 65

Dr. Chow-Sing LinModular Programming - CH 66

Functions with Simple Output Parameters Argument lists – Provide the communication links between the main function and its function subprograms – Enable a function to manipulate different data each time it is called When a function call executes, – Computer allocates memory space in the function data area for each formal parameter Dr. Chow-Sing Lin7Modular Programming - CH 6

Functions with Simple Output Parameters (Cont.) Example 6.1 (Page281) – Function separate finds the sign, whole number magnitude, and fractional parts of its first parameter – All the formal parameters of a function represent inputs to the function from the calling function – In function separate Input – num Output – signp – wholep – fracp Used to carry multiple results from function separate back to the function calling it Dr. Chow-Sing Lin8Modular Programming - CH 6

Dr. Chow-Sing LinModular Programming - CH 69

Functions with Simple Output Parameters (Cont.) Example 6.1 (Cont.) – Output parameter : char *signp (char * signp) Tells the compiler that signp will contain the address of a type chat variable pointer – signp is the address of a type char variable (the parameter signp is a pointer to a type char variable) – wholep and fracp are pointers to variables of types int and double Dr. Chow-Sing LinModular Programming - CH 610

Dr. Chow-Sing LinModular Programming - CH 611

Dr. Chow-Sing LinModular Programming - CH 612

Dr. Chow-Sing LinModular Programming - CH 613 Data areas of main and separate Set up by the function call statement separate (value, &sn, &wh1, &fr) ;

Functions with Simple Output Parameters (Cont.) Example 6.1 (Cont.) – Effect of & Operator on the Data Type of a Reference Dr. Chow-Sing LinModular Programming - CH 614 DeclarationData Type of xData Type of &x char xcharchar * (pointer to char) int xintint * (pointer to int) double xdoubledouble * (pointer to double)

Functions with Simple Output Parameters (Cont.) The statements in function separate that cause the return of results follow. – When the unary * operator is applied to reference that is of some pointer type, it has the effect of following the pointer referenced by its operand. Dr. Chow-Sing LinModular Programming - CH 615 *signp = ‘ - ’ ; *signp = ‘ ’ ; *signp = ‘ + ’ ; *wholep = floor(magnitude) ; *fracp = magnitude - *wholep ;

Functions with Simple Output Parameters (Cont.) Difference – a direct reference to a variable of type “pointer to int” – An indirect or “pointer-following” reference to the same variable using indirection operator Dr. Chow-Sing LinModular Programming - CH 616

Meaning of * symbol “*” in the declarations of the function’s formal – Multiplication – Pointer to char *signp Tell the compiler that the type of parameter signp is “pointer to char” (declaration) – Unary indirection operator Follow the pointer *signp – follow the pointer in signp Do not confuse the meaning of * in a reference with its meaning in a parameter declaration. Dr. Chow-Sing LinModular Programming - CH 617

Exercises for section 6.1 Write a prototype for a function sum_n_avg – include Three type double input parameters Two output parameters – The function computes the sum and the average of its three input arguments and relays its results through two output parameters Dr. Chow-Sing LinModular Programming - CH 618 sum_n_avg sump avgp n3 n2 n1

Exercises for section 6.1 (Cont.) Dr. Chow-Sing LinModular Programming - CH 619 void sum_n_avg(double n1, /* input numbers */ double n2, double n3, double *sump, /* output –sum of the three numbers */ double *avgp ) /* output –average of the numbers */ Answer

Multiple Calls to a Function with Input/output Parameters Use of a single parameter bring a data value into a function Carry a result value out of the function A function may be called more than once in a given program and process different data in each call Dr. Chow-Sing LinModular Programming - CH 620

Multiple Calls to a Function with Input/output Parameters (Cont.) Example 6.2 (Page289) – Main function gets three data values num1, num2, num3 – Sorting -- Increasing sequence The smallest value in num1 – The three calls to function order perform this sorting operation Dr. Chow-Sing LinModular Programming - CH 621

Dr. Chow-Sing LinModular Programming - CH 622

Dr. Chow-Sing LinModular Programming - CH 623

Multiple Calls to a Function with Input/output Parameters (Cont.) Example 6.2 (Cont.) – order(&num1, &num2) Stores the smaller of num1 and num2 in num1 The large in num2 – num1 = 7.5 ; num2 = 9.6 ; num3 = 5.5 order(&num1, &num2) – No change order(&num1, &num3) – Switches the values of num1 and num3 – num1 = 5.5 ; num3 = 7.5 Dr. Chow-Sing LinModular Programming - CH 624

Multiple Calls to a Function with Input/output Parameters (Cont.) Trace the main function execution Dr. Chow-Sing LinModular Programming - CH 625 Statementnum1num2num3Effect scanf(“…”, &num1, &num2, &num3); Enters data order(&num1, &num2);No change order(&num1, &num3); Switches num1 and num3 order(&num2, &num3); Switches num2 and num3 printf(“…”, num1, num2, num3);Displays

Dr. Chow-Sing LinModular Programming - CH 626 (7.5 > 5.5) true! Cause the 7.5 to be copied into the local variable temp Cause the 7.5 in the variable pointed to by smp to be replacer by 5.5 Copies temp variable 7.5 into the variable pointed by lgp

Dr. Chow-Sing LinModular Programming - CH 627

Exercises for Section 6.2 What would be the effect of the flowing sequence of calls to function order? – Hint : num1 = 8 ; num2 = 12 ; num3 = 10 – order(&num3, &num2); order(&num2, &num1); order(&num3, &num2); Dr. Chow-Sing LinModular Programming - CH 628

Exercises for Section 6.2 (Cont.) Answers Dr. Chow-Sing LinModular Programming - CH 629 Function callnum1num2num order(&num3, &num2); order(&num2, &num1);128 order(&num3, &num2);108 This sequence of calls to function order puts num1, num2, num3 in descending order (from largest to smallest).

Scope of Names Scope of a name – Refers to the region of a program where a particular meaning of a name is visible or can be referenced. Dr. Chow-Sing LinModular Programming - CH 630

Scope of Names (Cont.) Dr. Chow-Sing LinModular Programming - CH 631

Scope of Names (Cont.) Function fun_two can be called by functions one, main, and itself Function one can be called by the main function and itself – But not by function fun_two – Because one is used as a formal parameter name in function fun_two Dr. Chow-Sing LinModular Programming - CH 632

Scope of Names (Cont.) NameVisible in oneVisible in fun_twoVisible in main MAXyes LIMITyes mainyes localvar (in main)no yes one (the function)yesnoyes anarg (int)yesno secondyesno onelocalyesno fun_twoyes one (formal parameter)noyesno anarg (char)noyesno localvar (in fun_two)noyesno Dr. Chow-Sing LinModular Programming - CH 633

Formal Output Parameters as Actual Arguments When actual argument calls another function – a function needs to pass its own output parameter as an argument Function scan_fraction (not completed) Dr. Chow-Sing LinModular Programming - CH 634 Two output parameters Through function, returns the numerator and denominator of the function scanned

Formal Output Parameters as Actual Arguments (Cont.) Dr. Chow-Sing LinModular Programming - CH 635 Function scan_fraction needs to pass its output parameters to library function scanf which gets the needed numerator and denominator values

Formal Output Parameters as Actual Arguments (Cont.) Scans a data line representing a common fraction of the form – numerator / denominator numerator is an integer denominator is a positive integer The / symbol is a separator Use function get_int – Outer loop repeats until a valid fraction is scanned – Inner loop skips any characters at the end of the data line Dr. Chow-Sing LinModular Programming - CH 636

Formal Output Parameters as Actual Arguments (Cont.) Dr. Chow-Sing LinModular Programming - CH 637 nump&slash denomp Nump and denomp store addresses, we can use them directly in the call to scanf

Formal Output Parameters as Actual Arguments (Cont.) scanf(“%d %c%d”, nump, &slash, denomp); – scanf stores the first number scanned in the variable whose address is in nump – The slash character (possibly preceded by blanks) in local variable slash – The second number scanned in the variable whose address is in denomp – if statement validates the fraction If the data entry was unsuccessful – Setting the flag error to 1(true) Dr. Chow-Sing LinModular Programming - CH 638

Formal Output Parameters as Actual Arguments (Cont.) Data areas for scan_fraction and its Caller Dr. Chow-Sing LinModular Programming - CH 639

Passing an Argument x to Function some_fun Actual Argumen t Type Use in Calling Function Purpose in Called Function (some_fun) Formal Parameter Type Call to some_fun int char double local variable or input parameterinput parameter int char double some_fun(x) int char double local variable output or input/output parameter int * char * double * some_fun(&x) int * char * double * output or input/output parameter int * char * double * some_fun(x) int * char * double * output or input/output parameter input parameter int char double some_fun(*x) Dr. Chow-Sing LinModular Programming - CH 640

Figure 6.12 Structure Chart for Computing Solar Collecting Areas Size 1-41

Figure 6.13 Program to Approximate Solar collecting Area Size 1-42

Figure 6.13 Program to Approximate Solar collecting Area Size (cont’d) 1-43

Figure 6.13 Program to Approximate Solar collecting Area Size (cont’d) 1-44

Figure 6.13 Program to Approximate Solar collecting Area Size (cont’d) 1-45

Debugging and Testing a Program System Top-down testing – If a program contains one or more stubs The message printed by each stub when it is called provides a trace of the call sequence and allows the programmer to determine whether the flow of control within the program is correct – Between a main function and its subordinate functions Dr. Chow-Sing LinModular Programming - CH 646

Debugging and Testing a Program System (Cont.) Unit test – When a function is completed it can be substituted for its stub in the program We often perform a preliminary test of a new function before substitution – Because it is easier to locate and correct errors when dealing with a single function – A test of an individual function Dr. Chow-Sing LinModular Programming - CH 647

Debugging and Testing a Program System (Cont.) Bottom-up testing – The process of separately testing individual functions before inserting them in a program system System integration tests – Testing a system after replacing all its stubs with functions that have been pretested Dr. Chow-Sing LinModular Programming - CH 648

Common Programming Errors Each actual input argument must be of a type assigned to its corresponding formal parameter Each actual output argument must be of the same pointer data type as the corresponding formal parameter If an output parameter is not of a pointer type or if the calling function neglects to send a correct variable address – The function results will be incorrect Dr. Chow-Sing LinModular Programming - CH 649

Common Programming Errors (Cont.) If an identifier is referenced outside its scope – An undeclared symbol syntax error will result Dr. Chow-Sing LinModular Programming - CH 650