Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Advertisements

Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
CS1010 Programming Methodology
Lecture 2 Introduction to C Programming
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Functions (II) H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 24, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Computer Software & Software Development H&K Chapter 1 Instructor – Gokcen Cilingir Cpt S 121 (June 20, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 10.
Chapter 10 More on Modular Programming and 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.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Program Development and Design Using C++, Third Edition
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
CSE 220 – C Programming Pointers.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Functions Dr. Sajib Datta
Pointers.
INC 161 , CPE 100 Computer Programming
(2-1) Data Structures & The Basics of a Linked List I
Computer Science 210 Computer Organization
(2-1) Data Structures & The Basics of a Linked List I
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
A function with one argument
Pointers.
(1-1) C Review: Pointers, Arrays, Strings, & Structs
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Presentation transcript:

Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University

C. Hundhausen, A. O’Fallon, G. Cilingir2 Recall ‘pointers’ Let’s look at the definition of a pointer ◦ A variable that stores as its contents the address of another variable We should be able to use these address values to access a variable (memory location) indirectly Indirect access to these memory locations also will allow for modification to the contents 12 memory address 1000 p myint A variable (memory location) that stores an integer value A variable that stores an address of another variable (memory location) int myint = 12; int *p = &myint;

Recall the use of output parameters void divide (int num, int div, int *resultPtr, int *remainderPtr) { *resultPtr = num / div; *remainderPtr = number % divisor; } int main (void) { int num = 10, divisor = 3, result = 0, remainder = 0; divide (num, divisor, &result, &remainder); /*We can expect here that result and remainder variables now hold new values*/ } 1. Declare memory locations that will be accessed by the function with output parameters (here divide()) inside the caller of the function 2. Pass the addresses of these locations to the function with output parameters (here divide()) 3. Formal output parameters should be of type pointer in function header 4. Don’t forget to use the indirection / dereference operator while accessing these addresses

Pointer arithmetic can get tricky… int n1 = 5, n2 = 10, *p1 = NULL, *p2 = NULL; p1 = &n1; p2 = &n2; printf("%d %d %d %d\n", n1, n2, *p1, *p2); *p1 = 6; printf("%d %d %d %d\n", n1, n2, *p1, *p2); *p2 = *p1; printf("%d %d %d %d\n", n1, n2, *p1, *p2); *p2 = 7; printf("%d %d %d %d\n", n1, n2, *p1, *p2); p1 = p2; printf("%d %d %d %d\n", n1, n2, *p1, *p2); Output: n1 p n2 p n1 p n2 p n1 p n2 p n1 p n2 p n1 p n2 p2 200

GCD example revisited int gcdFinder (int n1, int n2) { int M = 0, N = 0, R = 0; //Assign M and N the values of the larger and smaller of the two positive integers, respectively. if(n1 > n2){ M = n1; N = n2; } else{ M = n2; N = n1; } //Euclid Algorithm applied here R = M % N; while (R != 0){ M = N; N = R; R = M % N; } return N; } We’d love to replace this part with a function call that takes n1 and n2 as input and determines the larger and the smaller among n1 and n2. Let’s use output parameters to define such a function!

GCD example revisited(2) int gcdFinder (int n1, int n2){ int M = 0, N = 0, R = 0; determineLarger_Smaller(n1,n2,&M,&N);//address of M and N is passed to function R = M % N; while (R != 0){ M = N; N = R; R = M % N; } return N; } void determineLarger_Smaller (int n1, int n2, int *larger, int *smaller){ if(n1 > n2){ *larger = n1; //indirectly accesses the location pointed by larger *smaller = n2; //indirectly accesses the location pointed by smaller } else{ *larger = n2; *smaller = n1; }

C. Hundhausen, A. O’Fallon7 Example problem (1) Write a function that prompts the reader for a date string in the form "mm/dd/yyyy." The function should return a valid month, day, and year as output parameters. Check date entered for validity, re- prompting the user for a valid date string if necessary.

C. Hundhausen, A. O’Fallon8 Example problem (2) Algorithm Pseudocode for get_date Set valid to true do prompt user to enter date string scan date string into month, seperator1, day, separator2, year if (status of scan is not equal to 5) or (separator1 is not a '/') or (separator2 is not a '/') or (day < 0) or (day > 31) or (month 12) or (year < 0) set valid to false tell user that date string is invalid end if while not valid return day, month, year end get_date

C. Hundhausen, A. O’Fallon9 Example problem (2) Start writing the prototype void get_date (int *month, int *day, int *year); A possible solution to get_date : void get_date (int *month, int *day, int *year) { int valid, status; char sep1, sep2; do { valid = 1; printf("Please enter a date (mm/dd/yyyy): "); status = scanf("%d %c%d %c%d", month,&sep1,day,&sep2,year); if (status < 5 || sep1 != '/' || sep2 != '/' || *day 31 || *month < 1 || *month > 12 || *year 9999) { valid = 0; printf("The date string you entered is invalid.\n"); } } while (!valid); } status flag, loop control variable

Example problem (3) main function as the test drive for get_date : int main(void) { int month, day, year; get_date(&month, &day, &year); printf(“Date: %d / %d / %d\n”, month, day, year); return 0; }

11 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.