Modular Programming 2 HK Chapter 6 Instructor Gokcen











- Slides: 11

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

Recall ‘pointers’ �Let’s look at the definition of a pointer ◦ A variable that stores as its contents the address of another variable int myint = 12; int *p = &myint; A variable that stores an address of another variable (memory location) p 1000 myint A variable (memory location) that stores an integer value 12 1000 memory address � 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 C. Hundhausen, A. O’Fallon, G. Cilingir 2

Recall the use of output parameters void divide (int num, int div, int *result. Ptr, int *remainder. Ptr) 3. Formal output parameters should be of type pointer in function header { *result. Ptr = num / div; *remainder. Ptr = number % divisor; } 4. Don’t forget to use the indirection / dereference operator while accessing these addresses 1. Declare memory locations that will be accessed by the function with output parameters (here divide()) inside the caller of the function 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*/ } 2. Pass the addresses of these locations to the function with output parameters (here divide())

Pointer arithmetic can get tricky… int n 1 = 5, n 2 = 10, *p 1 = NULL, *p 2 = NULL; p 1 = &n 1; p 2 = &n 2; printf("%d %dn", n 1, *p 1 = 6; printf("%d %dn", n 1, *p 2 = *p 1; printf("%d %dn", n 1, *p 2 = 7; printf("%d %dn", n 1, p 1 = p 2; printf("%d %dn", n 1, Output: 5 10 6666 6767 6777 p 1 100 n 2, *p 1, *p 2); n 2, *p 1, *p 2); p 1 100 p 1 200 n 1 n 2 10 5 100 200 n 1 n 2 10 6 100 200 n 1 n 2 6 6 100 200 n 1 n 2 7 6 100 200 p 2 200 p 2 200

GCD example revisited int gcd. Finder (int n 1, int n 2) { 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(n 1 > n 2){ N = n 2; We’d love to replace this part with a function call that takes n 1 and n 2 as input and determines the larger and the smaller among n 1 and n 2. M = n 2; Let’s use output parameters to define such a function! M = n 1; } else{ N = n 1; } //Euclid Algorithm applied here R = M % N; while (R != 0){ M = N; N = R; R = M % N; } return N; }

GCD example revisited(2) int gcd. Finder (int n 1, int n 2){ int M = 0, N = 0, R = 0; determine. Larger_Smaller(n 1, n 2, &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 determine. Larger_Smaller (int n 1, int n 2, int *larger, int *smaller){ if(n 1 > n 2){ *larger = n 1; //indirectly accesses the location pointed by larger *smaller = n 2; //indirectly accesses the location pointed by smaller } else{ *larger = n 2; *smaller = n 1; } }

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, reprompting the user for a valid date string if necessary. � C. Hundhausen, A. O’Fallon 7

Example problem (2) Algorithm Pseudocode for get_date Set valid to true do prompt user to enter date string scan date string into month, seperator 1, day, separator 2, year if (status of scan is not equal to 5) or (separator 1 is not a '/') or (separator 2 is not a '/') or (day < 0) or (day > 31) or (month < 1) 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’Fallon 8

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 sep 1, sep 2; do status flag, loop control variable { valid = 1; printf("Please enter a date (mm/dd/yyyy): "); status = scanf("%d %c%d", month, &sep 1, day, &sep 2, year); if (status < 5 || sep 1 != '/' || sep 2 != '/' || *day < 0 || *day > 31 || *month < 1 || *month > 12 || *year < 0 || *year > 9999) { valid = 0; printf("The date string you entered is invalid. n"); } } while (!valid); } C. Hundhausen, A. O’Fallon 9

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 / %dn”, month, day, year); return 0; }

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. 11