introduction to c programming biostatistics 615 815
play

Introduction to C++ Programming Biostatistics 615/815 - Lecture 2 . - PowerPoint PPT Presentation

. . September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang September 6th, 2012 Hyun Min Kang Introduction to C++ Programming Biostatistics 615/815 - Lecture 2 . . Summary Pointers 1 / 31 Syntax Data Types Recursion


  1. . . September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang September 6th, 2012 Hyun Min Kang Introduction to C++ Programming Biostatistics 615/815 - Lecture 2 . . Summary Pointers 1 / 31 Syntax Data Types Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  2. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang into outputs Last Lecture . Pointers Syntax Summary 2 / 31 . Recursion . . Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . • Algorithms are sequences of computational steps transforming inputs • Insertion Sort ✓ An intuitive sorting algorithm ✓ Loop invariant property ✓ Θ( n 2 ) time complexity ✓ Slower than default sort application in Linux. • A recursive algorithm for the Tower of Hanoi problem ✓ Recursion makes the algorithm simple ✓ Exponential time complexity • C++ Implementation of the above algorithms.

  3. . ) . . . 5 ( ) . . . . 7 ( . . . . . . . . . Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 6th, 2012 . 3 / 31 . Pointers . . . . . . . . . Recap Recursion Syntax Data Types . Summary Fill missing steps below to complete homework 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ssh uniqname@scs.itd.umich.edu 2 mkdir --p ~/Private/biostat615/hw0/ 3 cd ~/Private/biostat615/hw0/ 4 vi helloWorld.cpp (input the code) 6 vi towerOfHanoi.cpp (input the code) 8 rm *.o helloWorld towerOfHanoi 9 cd ../ 10 tar czvf uniqname.hw0.tar.gz hw0/ 11 scp uniqname@scs.itd.umich.edu:Private/biostat615/uniqname.hw0.tar.gz . (After logout)

  4. . Syntax September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang end end . Algorithm InsertionSort Summary Pointers 4 / 31 Data Types Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data : An unsorted list A [1 · · · n ] Result : The list A [1 · · · n ] is sorted for j = 2 to n do key = A [ j ] ; i = ( ) ; while i > 0 and A [ i ] > key do ( ) = ( ) ; i = i − 1 ; ( ) = key ;

  5. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang Today Summary Pointers . Syntax 5 / 31 . Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . • Hanoi Tower Example • Basic Data Types • Control Structures • Pointers and Functions • Fisher’s Exact Test

  6. . Syntax September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang practice yourself Next few lectures Summary . Pointers Data Types Recap . . . . . . . . . 6 / 31 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . • The class does NOT focus on teaching programming language itself • Expect to spend time to be familiar to programming languages ✓ Online reference : http://www.cplusplus.com/doc/tutorial/ ✓ Offline reference : C++ Primer Plus, 6th Edition • VERY important to practice writing code on your own. • Utilize office hours or after-class minutes for detailed questions in

  7. . Summary September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang How many moves are needed? Condition Output Move all the disks to the rightmost tower in the original order smallest to largest Input . . Problem . . Tower of Hanoi 7 / 31 . Syntax Data Types Recursion . Pointers Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . • A (leftmost) tower with n disks, ordered by size, • Two empty towers • One disk can be moved at a time. • A disk cannot be moved on top of a smaller disk.

  8. . . September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang http://www.youtube.com/watch?v=aGlt2G-DC8c A Working Example Summary Pointers Syntax 8 / 31 Data Types Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  9. • Move the other n • Move the largest disk to the rightmost tower • Move the other n . . . How to move the largest disk? . . . . . . . . disks from the leftmost to the middle tower disks from the middle to the rightmost tower Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 6th, 2012 another tower. . . Data Types . . . . . . . . . Recap Recursion 9 / 31 Syntax Pointers Summary Think Recursively . Key Idea . . . . . . . . . . . . . . . . . . . . . . . . . • Suppose that we know how to move n − 1 disks from one tower to • And concentrate on how to move the largest disk.

  10. . Summary September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang . . How to move the largest disk? . another tower. . . Key Idea . . Think Recursively 9 / 31 Recap . Syntax . Data Types . . Recursion . . . . . Pointers . . . . . . . . . . . . . . . . . . . . . . . . . • Suppose that we know how to move n − 1 disks from one tower to • And concentrate on how to move the largest disk. • Move the other n − 1 disks from the leftmost to the middle tower • Move the largest disk to the rightmost tower • Move the other n − 1 disks from the middle to the rightmost tower

  11. . Summary September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang end move disk n from s to d ; else do nothing; Result : n disks are moved from s to d . . Algorithm TowerOfHanoi . . A Recursive Algorithm for the Tower of Hanoi Problem 10 / 31 Recap . Syntax . Data Types . . Recursion . . . . . Pointers . . . . . . . . . . . . . . . . . . . . . . . . . Data : n : # disks, ( s , i , d ) : source, intermediate, destination towers if n == 0 then TowerOfHanoi ( n − 1 , s , d , i ); TowerOfHanoi ( n − 1 , i , s , d );

  12. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang How the Recursion Works Summary Pointers Syntax . 11 / 31 Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'(

  13. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang How the Recursion Works Summary Pointers . Syntax Recursion Recap . . . . . . . . . 11 / 31 . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !)#$#&#%'(

  14. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang How the Recursion Works Summary Pointers . Syntax 11 / 31 . Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !)#$#&#%'( !)#$#&#%'( !*#$#%#&'(

  15. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang How the Recursion Works Summary . Pointers Syntax 11 / 31 Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !*#$#%#&'( !*#$#%#&'( !+#$#&#%'(

  16. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang . Summary Pointers Syntax How the Recursion Works 11 / 31 Recursion . . Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !+#$#&#%'( !+#$#&#%'(

  17. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang . How the Recursion Works Summary Pointers Syntax 11 / 31 . Recursion Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !+#$#&#%'( !+#$#&#%'( !+#$#&#%'( ,-./*( $(01(&(

  18. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang . How the Recursion Works Summary Pointers Syntax 11 / 31 Recap Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !+#$#&#%'( !+#$#&#%'( !+#$#&#%'( !+#$#&#%'( !+#%#$#&'( ,-./*( ,-./*( $(01(&( $(01(&(

  19. . Data Types September 6th, 2012 Biostatistics 615/815 - Lecture 2 Hyun Min Kang . How the Recursion Works Summary Pointers Syntax 11 / 31 . Recursion . . Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !"#$#%#&'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !)#$#&#%'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !*#$#%#&'( !+#$#&#%'( !+#$#&#%'( !+#$#&#%'( !+#$#&#%'( !+#$#&#%'( !+#%#$#&'( !+#%#$#&'( ,-./*( ,-./*( ,-./*( $(01(&( $(01(&( $(01(&(

Recommend


More recommend