. . September 8th, 2011 Biostatistics 615/815 - Lecture 2 Hyun Min Kang September 8th, 2011 Hyun Min Kang Introduction to C++ Programming Biostatistics 615/815 - Lecture 2 . . Summary Pointers 1 / 33 Syntax Data Types Implementation Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. statistical methods. . 1 Equip the ability to IMPLEMENT computational/statistical IDEAS into working SOFTWARE PROGRAMs . . 2 Learn COMPUTATIONAL COST management in developing . BIOSTAT615/815 - Objectives . 3 Understand NUMERICAL and RANDOMIZED ALGORITHMS for statistical inference Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 . Summary . Recap . . . . . . . . . . . Implementation Data Types Syntax Pointers 2 / 33 . . . . . . . . . . . . . . . . . . . . . . . . .
. Pointers September 8th, 2011 Biostatistics 615/815 - Lecture 2 Hyun Min Kang (adapted from Jeff Erickson(UIUC)’s class notes) end Sing ”Old MacDonald had a farm, E I E I O.”; end Sing ”Old MacDonald had a farm, E I E I O”; Result : An “Old MacDonald” Song with animals and noises . Summary Algorithm SingOldMacDonald 3 / 33 . Data Types . . Implementation . . . . . . . . Syntax Recap . . . . . . . . . . . . . . . . . . . . . . . . . Data : animals [1 · · · n ] , noises [1 · · · n ] for i = 1 to n do Sing ”And on that farm he had some animals [ i ] , E I E I O”; Sing ”With a noises [ i ] noises [ i ] here, and a noises [ i ] noises [ i ] there”; Sing ”Here a noise [ i ] , there a noise [ i ] , everywhere a noise [ i ] noise [ i ] ”; for j = i − 1 downto 1 do Sing ” noise [ j ] noise [ j ] here, noise [ j ] noise [ j ] there”; Sing ”Here a noise [ j ] , there a noise [ j ] , everywhere a noise [ j ] noise [ j ] ”;
. Data Types September 8th, 2011 Biostatistics 615/815 - Lecture 2 Hyun Min Kang sorted in order. Key Idea of Insertion Sort Summary Pointers . Syntax Recap Implementation . . . . . . . . . . . 4 / 33 . . . . . . . . . . . . . . . . . . . . . . . . . • For k -th step, assume that elements a [1] , · · · , a [ k − 1] are already • Locate a [ k ] between index 1 , · · · , k so that a [1] , · · · , a [ k ] are in order • Move the focus to k + 1 -th element and repeat the same step
. Syntax September 8th, 2011 Biostatistics 615/815 - Lecture 2 Hyun Min Kang end end Algorithm InsertionSort . Pointers Summary Data Types . . . . . . . . . . . 5 / 33 Implementation 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 = j − 1 ; while i > 0 and A [ i ] > key do A [ i + 1] = A [ i ] ; i = i − 1 ; A [ i + 1] = key ;
. Condition Problem . . Input smallest to largest Output Move all the disks to the rightmost tower in the original order . Recap - Tower of Hanoi Problem Key Idea - Think Recursively . . Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 . . Summary Implementation . . . . . . . . . . . Recap 6 / 33 Data Types Syntax Pointers . . . . . . . . . . . . . . . . . . . . . . . . . • 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. • 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
. Summary September 8th, 2011 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 7 / 33 Recap . Syntax . Data Types . . Implementation . . . . . . . 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 );
• The class does NOT focus on teaching programming language itself • Expect to spend time to be familar to programming languages • VERY important to practice writing code on your own. • Utilize office hours or after-class minutes for detailed questions in Next few lectures . . . . . . . . . . yourself Online reference : http://www.cplusplus.com/doc/tutorial/ Offline reference : C++ Primer Plus, 5th Edition practice Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 . 8 / 33 . . . . . . . . Implementation Data Types . Syntax . Pointers . Summary Today’s and Next Lectures . Today . . Recap . . . . . . . . . . . . . . . . . . . . . . . . . • Basic Data Types • Control Structures • Pointers and Functions
. Summary September 8th, 2011 Biostatistics 615/815 - Lecture 2 Hyun Min Kang practice yourself . . Next few lectures . . . Today . Today’s and Next Lectures . 8 / 33 Implementation . . . . . . . . . . . Pointers Recap Data Types Syntax . . . . . . . . . . . . . . . . . . . . . . . . . • Basic Data Types • Control Structures • Pointers and Functions • The class does NOT focus on teaching programming language itself • Expect to spend time to be familar to programming languages ✓ Online reference : http://www.cplusplus.com/doc/tutorial/ ✓ Offline reference : C++ Primer Plus, 5th Edition • VERY important to practice writing code on your own. • Utilize office hours or after-class minutes for detailed questions in
. Pointers September 8th, 2011 Biostatistics 615/815 - Lecture 2 Hyun Min Kang recommended. UNIX interface. so it would be good to be familiar with it. 1 UNIX / gcc environment . . Example C++ Development Environment Summary . 9 / 33 . . . . . . . . Data Types Implementation . . Recap . Syntax . . . . . . . . . . . . . . . . . . . . . . . . . • Instructor’s preference • UNIX environment will be commonly used in large-scale data analysis, • Ways to set up UNIX environment • Install Linux (e.g. Ubuntu) locally to your computer • Download and install Xcode in Mac OS X, and use terminal to access • Install Cygwin to a windows machine (mimics UNIX environment) • Connect to U-M login service via SSH using PuTTY or similar software (Refer to http://www.itd.umich.edu/login/ for details) • Learning Unix-cultured editors such as vi or emacs is also
. . . 1 UNIX / gcc environment . . 2 Windows / Microsoft Visual C++ . 3 Windows / Borland C++ Builder Example C++ Development Environment . . 4 Mac OS X / Xcode development environment Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 . Summary . Recap . . . . . . . . . . . Implementation Data Types Syntax Pointers 10 / 33 . . . . . . . . . . . . . . . . . . . . . . . . .
user@host:~/$ g++ -o helloWorld helloWorld.cpp user@host:~/$ ./helloWorld Hello, World . Running helloWorld . . . . . . . . . . . . . . . . . . Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 Compiling helloWorld.cpp 11 / 33 . Recap Implementation Data Types Syntax . . . . . . . Pointers . Summary Getting Started with C++ . Writing helloWorld.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #include <iostream> // import input/output handling library int main(int argc, char** argv) { std::cout << "Hello, World" << std::endl; return 0; // program exits normally }
user@host:~/$ ./helloWorld Hello, World . . Compiling helloWorld.cpp . . . Running helloWorld . . . . . . . . Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 . 11 / 33 . . Data Types Syntax Recap Pointers . . . . . . . Summary Getting Started with C++ . Writing helloWorld.cpp . . . . Implementation . . . . . . . . . . . . . . . . . . . . . . . . . #include <iostream> // import input/output handling library int main(int argc, char** argv) { std::cout << "Hello, World" << std::endl; return 0; // program exits normally } user@host:~/$ g++ -o helloWorld helloWorld.cpp
. . Writing helloWorld.cpp . . . Compiling helloWorld.cpp . . Getting Started with C++ Running helloWorld . . Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 . . Summary Data Types . . . . . . . . . . . Recap Implementation 11 / 33 Syntax Pointers . . . . . . . . . . . . . . . . . . . . . . . . . #include <iostream> // import input/output handling library int main(int argc, char** argv) { std::cout << "Hello, World" << std::endl; return 0; // program exits normally } user@host:~/$ g++ -o helloWorld helloWorld.cpp user@host:~/$ ./helloWorld Hello, World
Recommend
More recommend