1 / 52

ecs30 Winter 2012: Programming and Problem Solving # 16: Chapters 9~10, Strings to Recurrsion

ecs30 Winter 2012: Programming and Problem Solving # 16: Chapters 9~10, Strings to Recurrsion. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. HW#6.1.

lecea
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 16: Chapters 9~10, Strings to Recurrsion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ecs30 Winter 2012:Programming and Problem Solving#16: Chapters 9~10, Strings to Recurrsion Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #16

  2. HW#6.1 • Game of Life – please read the wikipedia page to understand the game itself • A partial/unfinished program for you to complete/finish: • Learn to understand code/program by others • Learn how to complete or modify other’s code. • Reference executables (arch – 64/32 bits) • Executables without source code (reverse engineering) ecs30 Winter 2012 Lecture #16

  3. HW#6.2 – Common Suffix • I just posted a sample driver • It’s like yet another string utility function. • You should build up your capability similarly in writing/developing any utility functions in C. ecs30 Winter 2012 Lecture #16

  4. 0,0 0,1 0,2 0,3 char m[4][4]; 1,0 1,1 1,2 1,3 void printMatrix (char m[][4]) { m[2][1] … } 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 m == &(m[0][0]) m[x][y] ~ *((char *) m + x*columns + y) 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 How many rows I should skip? ecs30 Winter 2012 Lecture #16

  5. 0,0 0,1 0,2 0,3 char m[4][4]; 1,0 1,1 1,2 1,3 void printMatrix (char m[][4]) { m[2][1] … } 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 m == &(m[0][0]) &(m[x][y]) ~ ((char *) m + x*columns + y) 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2 2,3 3,0 3,1 3,2 3,3 How many rows I should skip? ecs30 Winter 2012 Lecture #16

  6. m[x][y] ~ *((char *) m + x*columns + y) ((int) m) + ((x*columns) + y) * sizeof(char) m[x][y] ~ *((int *) m + x*columns + y) ((int) m) + ((x*columns) + y) * sizeof(int) ecs30 Winter 2012 Lecture #16

  7. ecs30 Winter 2012 Lecture #16

  8. ecs30 Winter 2012 Lecture #16

  9. ecs30 Winter 2012 Lecture #16

  10. ecs30 Winter 2012 Lecture #16

  11. GNU debugger • The -g flag • gdb a.out • run (r) • break (b) • next (n), nexti (ni) • step (s) • print (p) • display • continue (c) • watch • x ecs30 Winter 2012 Lecture #16

  12. gdb Examples $ script hw7_1_gdb.script • display i • c • n 2 • ni 9 • s • watch i == 3 • watch i != 0 • x /4 0xbffff862 • p i • p s • p s[14] • p s[12] = ‘\0’ • p i = 0 • b main • b strlen • b 12 http://www.digilife.be/quickreferences/QRC/GDB Quick Reference.pdf http://www.hlrs.de/organization/amt/services/tools/debugger/gdb/doc/gdb-6.3.pdf ecs30 Winter 2012 Lecture #16

  13. Recursion • A function calls itself, maybe with a different set of parameter values… • Different sets of local variables… (n-1)! N! ecs30 Winter 2012 Lecture #16

  14. (m * n) ~ (m + m * (n - 1)) ecs30 Winter 2012 Lecture #16

  15. ecs30 Winter 2012 Lecture #16

  16. It gotta stop… at some point!! Exit Strategy/Condition! ecs30 Winter 2012 Lecture #16

  17. ecs30 Winter 2012 Lecture #16

  18. ecs30 Winter 2012 Lecture #16

  19. The Towers of Hanoi http://en.wikipedia.org/wiki/Tower_of_Hanoi ecs30 Winter 2012 Lecture #16

  20. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi. Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order: ecs30 Winter 2012 Lecture #16

  21. A Legend The priests were to transfer the disks from the first needle to the second needle, using the third as necessary. But they could only moveone disk at a time, and could never put a larger disk on top of a smaller one. When they completed this task, the world would end! ecs30 Winter 2012 Lecture #16

  22. To Illustrate For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... Since we can only move one disk at a time, we move the top disk from A to B. ecs30 Winter 2012 Lecture #16

  23. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to C. ecs30 Winter 2012 Lecture #16

  24. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from B to C. ecs30 Winter 2012 Lecture #16

  25. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to B. ecs30 Winter 2012 Lecture #16

  26. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from C to A. ecs30 Winter 2012 Lecture #16

  27. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from C to B. ecs30 Winter 2012 Lecture #16

  28. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... We then move the top disk from A to B. ecs30 Winter 2012 Lecture #16

  29. Example For simplicity, suppose there were just 3 disks, and we’ll refer to the three needles as A, B, and C... and we’re done! The problem gets more difficult as the number of disks increases... ecs30 Winter 2012 Lecture #16

  30. Our Problem Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks. While quite difficult to solve iteratively, this problem has a simple and elegant recursive solution. ecs30 Winter 2012 Lecture #16

  31. Analysis The tower function: void tower (char from_peg, char to_peg, char aux_peg, int n) { … } ecs30 Winter 2012 Lecture #16

  32. void tower (char from_peg, char to_peg, char aux_peg, int n) { … } int main(void) { int n; scanf(“%d”, &n); tower(‘A’, ‘B’, ‘C’, n); } ecs30 Winter 2012 Lecture #16

  33. Testing The Hanoi Towers Enter how many disks: 2 move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #16

  34. Testing The Hanoi Towers Enter how many disks: 3 move a disk from A to B move a disk from A to C move a disk from B to C move a disk from A to B move a disk from C to A move a disk from C to B move a disk from A to B ecs30 Winter 2012 Lecture #16

  35. Testing The Hanoi Towers Enter how many disks: 4 move a disk from A to B move a disk from C to B move a disk from A to C move a disk from B to A move a disk from B to C move a disk from A to C move a disk from A to B move a disk from C to B move a disk from C to A move a disk from B to A move a disk from C to B move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #16

  36. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { … } ecs30 Winter 2012 Lecture #16

  37. Design: “Exit Condition” Basis: What is an instance of the problem that is trivial? ® n == 1 Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest. ecs30 Winter 2012 Lecture #16

  38. Design Basis: What is an instance of the problem that is trivial? ® n == 1 Since this base case could occur when the disk is on any needle, we simply output the instruction to move the top disk from src to dest. ecs30 Winter 2012 Lecture #16

  39. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); … } ecs30 Winter 2012 Lecture #16

  40. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? a. Recursively move n-1 disks from src to aux. ecs30 Winter 2012 Lecture #16

  41. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? b. Move the one remaining disk from src to dest. ecs30 Winter 2012 Lecture #16

  42. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? c. Recursively move n-1 disks from aux to dest... ecs30 Winter 2012 Lecture #16

  43. Design (Ct’d) Induction Step: n > 1 ® How can recursion help us out? d. We’re done! ecs30 Winter 2012 Lecture #16

  44. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); … } ecs30 Winter 2012 Lecture #16

  45. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #16

  46. The Tower FunctionFigure 10.24, page 540 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #16

  47. The Tower FunctionFigure 10.24, page 540 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move a disk from %c to %c\n”, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #16

  48. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move disk %d from %c to %c\n”, n, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #16

  49. Testing The Hanoi Towers Enter how many disks: 2 move disk 1 from A to C move disk 2 from A to B move disk 1 from C to B ecs30 Winter 2012 Lecture #16

  50. HW#7 • Tower of Hanoi with FOUR Pegs ecs30 Winter 2012 Lecture #16

More Related