Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University."— Presentation transcript:

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

2 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;

3 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

4 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: 5 10 6 10 6 6 6 7 6 7 7 7 5 100 n1 p1 100 10 200 n2 p2 200 6 100 n1 p1 100 6 200 n2 p2 200 6 100 n1 p1 100 7 200 n2 p2 200 6 n1 p1 100 7 200 n2 p2 200 6 100 n1 p1 100 10 200 n2 p2 200

5 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!

6 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; }

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

8 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

9 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

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


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

Similar presentations


Ads by Google