Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Similar documents
Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Biostatistics 615/815 - Statistical Computing Lecture 1 : Introduction to Statistical Computing

Biostatistics 615/815 Statistical Computing

Biostatistics 615/815 - Lecture 3 C++ Basics & Implementing Fisher s Exact Test

Biostatistics 615/815 - Lecture 3 C++ Basics & Implementing Fisher s Exact Test

Biostatistics 615/815 Implementing Fisher s Exact Test

Biostatistics 615/815 - Statistical Computing Lecture 1 : Introduction to Statistical Computing

Recap - helloworld - Have all tried?

Biostatistics 615/815 Lecture 5: More on STLs, and Divide and Conquer Algorithms

Biostatistics 615/815 Lecture 4: User-defined Data Types, Standard Template Library, and Divide and Conquer Algorithms

Biostatistics 615/815 Lecture 6: Linear Sorting Algorithms and Elementary Data Structures

Biostatistics 615/815 Lecture 5: Divide and Conquer Algorithms Sorting Algorithms

Announcements. Biostatistics 615/815 Lecture 5: More on STLs, and Divide and Conquer Algorithms. Recap on STL : vector.

Computer Science II Lecture 1 Introduction and Background

Lab 1: Introduction to C++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

COMP322 - Introduction to C++ Lecture 01 - Introduction

CS2141 Software Development using C/C++ C++ Basics

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Lecture 03 Bits, Bytes and Data Types

Lecture 2, September 4

Tutorial-2a: First steps with C++ programming

C++ for Python Programmers

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 11 java track: lecture 1

Eclipse CDT Tutorial. Eclipse CDT Homepage: Tutorial written by: James D Aniello

Scientific Computing

Course Outline. Introduction to java

Lecture 3. More About C

Procedures, Parameters, Values and Variables. Steven R. Bagley

Compiling C/C++ programs

CE221 Programming in C++ Part 1 Introduction

Few reminders and demos

PROGRAMMING IN C++ CVIČENÍ

Introduction to C ++

Announcements. Biostatistics 615/815 Lecture 7: Data Structures. Recap : Radix sort. Recap : Elementary data structures. Another good and bad news

High Performance Computing

Biostatistics 615/815 Lecture 13: Numerical Optimization

Computational Physics Operating systems

Introduction to C++ Introduction to C++ 1

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

C++ Programming for Non-C Programmers. Supplement

CSE 351. Introduction & Course Tools

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Work relative to other classes

Numbers. John Perry. Spring 2017

Visual C# Instructor s Manual Table of Contents

15. Pointers, Algorithms, Iterators and Containers II

Program Organization and Comments

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ for Java Programmers

Chapter 2 Basic Elements of C++

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

MEIN 50010: Python Introduction

CSC111 Computer Science II

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Chapter 1 & 2 Introduction to C Language

PRINCIPLES OF OPERATING SYSTEMS

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C: How to Program. Week /Mar/05

Lecture 5 Files and Streams

Final CSE 131B Spring 2004

C++ Programming: From Problem Analysis to Program Design, Third Edition

UNIT- 3 Introduction to C++

CS 253: Intro to Systems Programming 1/21

Introduction to Programming using C++

Getting started with Java

ENERGY 211 / CME 211. Evolution

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Introduction to Programming

Page 1. Agenda. Programming Languages. C Compilation Process

ECE 2036 Lab 1: Introduction to Software Objects

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Programming refresher and intro to C programming

Cours de C++ Introduction

Motivation was to facilitate development of systems software, especially OS development.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Chapter 3 - Functions

211: Computer Architecture Summer 2016

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

CSCE 110 PROGRAMMING FUNDAMENTALS

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS61C : Machine Structures

Variables and literals

Introduction to C An overview of the programming language C, syntax, data types and input/output

Objectives. In this chapter, you will:

LECTURE 3 C++ Basics Part 2

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

12. Pointers Address-of operator (&)

Transcription:

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming Hyun Min Kang September 8th, 2011 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 1 / 47

BIOSTAT615/815 - Objectives 1 Equip the ability to IMPLEMENT computational/statistical IDEAS into working SOFTWARE PROGRAMs 2 Learn COMPUTATIONAL COST management in developing statistical methods 3 Understand NUMERICAL and RANDOMIZED ALGORITHMS for statistical inference Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 2 / 47

Algorithm SingOldMacDonald Data: animals[1 n], noises[1 n] Result: An Old MacDonald Song with animals and noises for i = 1 to n do Sing Old MacDonald had a farm, E I E I O ; 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] ; end Sing Old MacDonald had a farm, E I E I O ; end (adapted from Jeff Erickson(UIUC) s class notes) Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 3 / 47

Key Idea of Insertion Sort For k-th step, assume that elements a[1],, a[k 1] are already sorted in order 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 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 4 / 47

Algorithm InsertionSort 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; end A[i + 1] = key; end Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 5 / 47

Recap - Tower of Hanoi Problem Problem Input A (leftmost) tower with n disks, ordered by size, smallest to largest Two empty towers Output Move all the disks to the rightmost tower in the original order Condition One disk can be moved at a time A disk cannot be moved on top of a smaller disk Key Idea - Think Recursively 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 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 6 / 47

A Recursive Algorithm for the Tower of Hanoi Problem Algorithm TowerOfHanoi Data: n : # disks, (s, i, d) : source, intermediate, destination towers Result: n disks are moved from s to d if n == 0 then do nothing; else TowerOfHanoi(n 1, s, d, i); move disk n from s to d; TowerOfHanoi(n 1, i, s, d); end Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 7 / 47

Today s and Next Lectures Today Basic Control Structures and Functions Next few lectures The class does NOT focus on teaching programming language itself Expect to spend time to be familar to programming languages yourself Online reference : http://wwwcpluspluscom/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 practice Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 8 / 47

Example C++ Development Environment 1 UNIX / gcc environment Instructor s preference UNIX environment will be commonly used in large-scale data analysis, so it would be good to be familiar with it Ways to set up UNIX environment Install Linux (eg Ubuntu) locally to your computer Download and install Xcode in Mac OS X, and use terminal to access UNIX interface 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://wwwitdumichedu/login/for details) Learning Unix-cultured editors such as vi or emacs is also recommended Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 9 / 47

Example C++ Development Environment 1 UNIX / gcc environment 2 Windows / Microsoft Visual C++ 3 Windows / Borland C++ Builder 4 Mac OS X / Xcode development environment Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 10 / 47

Getting Started with C++ Writing helloworldcpp #include <iostream> // import input/output handling library int main(int argc, char** argv) { } std::cout << "Hello, World" << std::endl; return 0; // program exits normally Compiling helloworldcpp user@host:~/$ g++ -o helloworld helloworldcpp Running helloworld user@host:~/$ /helloworld Hello, World Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 11 / 47

How helloworld works main() : First function to be called // type of return value is integer // return value of main() function is program exit code // 0 is normal exit code and the others are abnormal exit codes int // name (identifier) of function is 'main' // 'main' is a special function, invoked at the beginning of a program main ( // function arguments are surrounded by parentheses ) { } int argc, char** argv // number of command line arguments // list of command line arguments - will explain later // function body goes here return 0; // return normal exit code Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 12 / 47

How helloworld works Using iostream to output strings to console // includes standard library for handling I/Os (inputs/outputs) // std::cout and std::endl cannot be recognized without including <iostream> #include <iostream> int main (int argc, char** argv) { } std::cout // standard output stream - messages are printed to console << // insertion operator : appends the next value to the output stream "Hello, World" // string appended to std::cout via operator<< << // insertion operator : appends the next value to the output stream std::endl; // end-of-line appended to std::cout via operator<< return 0; Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 13 / 47

Implementing TowerOfHanoi Algorithm in C++ towerofhanoicpp #include <iostream> #include <cstdlib> // recursive function of towerofhanoi algorithm void towerofhanoi(int n, int s, int i, int d) { } } if ( n > 0 ) { } towerofhanoi(n-1,s,d,i); // recursively move n-1 disks from s to i // Move n-th disk from s to d std::cout << "Disk " << n << " : " << s << " -> " << d << std::endl; towerofhanoi(n-1,i,s,d); // recursively move n-1 disks from i to d // main function int main(int argc, char** argv) { int ndisks = atoi(argv[1]); // convert input argument to integer towerofhanoi(ndisks, 1, 2, 3); // run TowerOfHanoi(n=nDisks, s=1, i=2, d=3) return 0; Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 14 / 47

Running TowerOfHanoi Running towerofhanoi user@host:~/$ /towerofhanoi 3 Disk 1 : 1 -> 3 Disk 2 : 1 -> 2 Disk 1 : 3 -> 2 Disk 3 : 1 -> 3 Disk 1 : 2 -> 1 Disk 2 : 2 -> 3 Disk 1 : 1 -> 3 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 15 / 47

Homework 0 Implement the following two programs and send the output screenshots to the instructor (hmkang at umich dot edu) by E-mail 1 HelloWorldcpp 2 TowerOfHanoicpp Briefly describe your operating system and C++ development environment with your submission This homework will not be graded, but mandatory to submit for everyone who wants to take the class for credit No due date, but homework 0 must be submitted prior to submitting any other homework Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 16 / 47

Declaring Variables Variable Declaration and Assignment int foo; // declare a variable foo = 5; // assign a value to a variable int foo = 5; // declararion + assignment Variable Names int poodle; // valid int Poodle; // valid and distinct from poodle int my_stars3; // valid to include underscores and digits int 4ever; // invalid because it starts with a digit int double; // invalid because double is C++ keyword int honky-tonk; // invalid -- no hyphens allowed Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 17 / 47

Basic Digital Units bit A single binary digit number which can represent either 0 or 1 byte A collection of 8 bits which can represent 256(= 2 8 ) unique numbers One character can typically be stored within one byte word An ambiguous term for the natural unit of data in each processor Typically, a word corresponds to the number of bits to represent a memory address In 32-bit address scheme which can represent up to 4 gigabytes, 32 bits (4 bytes) are spent to represent a memory address In 64-bit address scheme, up to 18 exabytes can be represented by using 64 bits (8 bytes) to represent a memory address Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 18 / 47

Signed Integer Types char foo; // 8 bits (1 byte) : -128 <= foo <= 128 short foo; // 16 bits (2 bytes) : -32,768 <= foo <= 32,767 int foo; // Mostly 32 bits (4 bytes) : -2,147,483,648 <= foo <= 2,147,483,647 long foo; // 32 bits (4 bytes) : -2,147,483,648 <= foo <= 2,147,483,647 long long foo; // 64 bits short foo = 0; foo = foo - 1; // foo is -1 Unsigned Integer Types unsigned char foo; 8 bits (1 byte) : 0 <= foo <= 255 unsigned short foo; // 16 bits (2 bytes) : 0 <= foo <= 65,535 unsigned int foo; // Mostly 32 bits (4 bytes) : 0 <= foo <= 4,294,967,295 unsigned long foo; // 32 bits (4 bytes) : 0 <= foo <= 4,294,967,295 unsigned long long foo; // 64 bits unsigned short foo = 0; foo = foo - 1; // foo is 65,535 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 19 / 47

Floating Point Numbers Comparisons Type float double long double Precision Single Double Quadruple Size 32 bits 64 bits 128 bits (in most modern OS) 4 bytes 8 bytes 16 bytes Sign 1 bit 1 bit 1 bit Exponent 8 bits 11 bits 15 bits Fraction 23 bits 52 bits 112 bits (# decimal digits) 72 16 34 Minimum (>0) 12 10 38 22 10 308 34 10 4932 Maximum 34 10 38 18 10 308 12 10 4932 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 20 / 47

Handling Floating Point Precision Carefully precisionexamplecpp #include <iostream> int main(int argc, char** argv) { } float smallfloat = 1e-8; // a small value float largefloat = 1; // difference in 8 (>72) decimal figures std::cout << smallfloat << std::endl; // "1e-08" is printed smallfloat = smallfloat + largefloat; // smallfloat becomes exactly 1 smallfloat = smallfloat - largefloat; // smallfloat becomes exactly 0 std::cout << smallfloat << std::endl; // "0" is printed // similar thing happens for doubles (eg 1e-20 vs 1) return 0; Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 21 / 47

Basics of Arrays and Strings Array int A[] = {3,6,8}; // A[] can be replaced with A[3] std::cout << "A[0] = " << A[0] << std::endl; // prints 3 std::cout << "A[1] = " << A[1] << std::endl; // prints 6 std::cout << "A[2] = " << A[2] << std::endl; // prints 8 String as an array of characters char s[] = "Hello, world"; // or equivalently, char* s = "Hello, world" std::cout << "s[0] = " << s[0] << std::endl; // prints 'H' std::cout << "s[5] = " << s[5] << std::endl; // prints ',' std::cout << "s = " << s << std::endl; // prints "Hello, world" Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 22 / 47

- and Precisions Each data type consumes different amount of memory For example, 1GB can store a billion characters, and 125 million double precision floating point numbers To store a human genome as character types, 3GB will be consumed, but 12GB will be needed if each nucleotide is represented as an integer type Precision is not unlimited Unexpected results may happen if the operations require too many significant digits Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 23 / 47

Assignment and Arithmetic Operators int a = 3, b = 2; // valid int c = a + b; // addition : c == 5 int d = a - b; // subtraction : d == 1 int e = a * b; // multiplication : e == 6 int f = a / b; // division (int) generates quotient : f == 1 int g = a + b * c; // precedence - add after multiply : g == 3 + 2 * 5 == 13 a = a + 2; // a == 5 a += 2; // a == 7 ++a; // a == 8 a = b = c = e; // a == b == c == e == 6 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 24 / 47

Comparison Operators and Conditional Statements int a = 2, b = 2, c = 3; std::cout << (a == b) << std::endl; // prints 1 (true) std::cout << (a == c) << std::endl; // prints 0 (false) std::cout << (a!= c) << std::endl; // prints 1 (true) if ( a == b ) { // conditional statment std::cout << "a and b are same" << std::endl; } else { std::cout << "a and b are different" << std::endl; } std::cout << "a and b are " << (a == b? "same" : "different") << std::endl << "a is " << (a < b? "less" : "not less") << " than b" << std::endl << "a is " << (a <= b? "equal or less" : "greater") << " than b" << std::endl; Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 25 / 47

Loops while loop int i=0; } // initialize the key value while( i < 10 ) { // evaluate the loop condition std::cout << "i = " << i << std::endl; // prints i=0 i=9 ++i; // update the key value for loop for(int i=0; i < 10; ++i) { // initialize, evaluate, update } std::cout << "i = " << i << std::endl; // prints i=0 i=9 Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 26 / 47

%&;&'()*+&!""#$%%& '()*+& '()*,& '()*-& '()*& '()*/& '()*0& 1023$& 456& 476& 486& 486& 496& 4:'6& Another while loop char* s = "HELLO"; // array of {'H','E','L','L','O','\0'} while ( *s!= '\0' ) { } // *s access the character value pointed by s std::cout << *s << std::endl; // prints 'H','E','L','L','O' at each line ++s; // advancing the pointer by one; points to the next element Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 27 / 47

and Loops while loop char* s = "HELLO"; // array of {'H','E','L','L','O','\0'} while ( *s!= '\0' ) { } std::cout << *s << std::endl; // prints 'H','E','L','L','O' at each line ++s; // advancing the pointer by one for loop // initialize array within for loop for(char* s = "HELLO"; *s!= '\0'; ++s) { } std::cout << *s << std::endl; // prints 'H','E','L','L','O' at each line Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 28 / 47

are complicated, but important int A[] = {3,6,8}; // A is a pointer to a constant address int* p = A; // p and A are containing the same address std::cout << p[0] << std::endl; // prints 3 because p[0] == A[0] == 3 std::cout << *p << std::endl; // prints 3 because *p == p[0] std::cout << p[2] << std::endl; // prints 8 because p[2] == A[2] == 8 std::cout << *(p+2) << std::endl; // prints 8 because *(p+2) == p[2] int b = 3; // regular integer value int* q = &b; // the value of q is the address of b b = 4; // the value of b is changed std::cout << *q << std::endl; // *q == b == 4 char s[] = "Hello"; char *t = s; std::cout << t << std::endl; // prints "Hello" char *u = &s[3]; // &s[3] is equivalent to s + 3 std::cout << u << std::endl; // prints "lo" Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 29 / 47

and References int a = 2; int& ra = a; // reference to a int* pa = &a; // pointer to a int b = a; // copy of a ++a; // increment a std::cout << a << std::endl; // prints 3 std::cout << ra << std::endl; // prints 3 std::cout << *pa << std::endl; // prints 3 std::cout << b << std::endl; // prints 2 int* pb; // valid, but what pb points to is undefined int* pc = NULL; // valid, pc points to nothing std::cout << *pc << std::endl; // Run-time error : pc cannot be dereferenced int& rb; // invalid reference must refer to something int& rb = 2; // invalid reference must refer to a variable Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 30 / 47

so far Algorithms are computational steps towerofhanoi utilizing recursions insertionsort Simple but a slow sorting algorithm Loop invariant property Data types and floating-point precisions Operators, if, for, and while statements Arrays and strings and References At Home : Reading material for novice C++ users : http://wwwcpluspluscom/doc/tutorial/ Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 31 / 47

At Home : Write, Compile and Run The following list of programs helloworldcpp towerofhanoicpp callbyvalrefcpp precisionexamplecpp How to Write Notepad, Vim, Emacs, Eclipse, VisualStudio, etc Compile g++ -Wall -o [progname] [progname]cpp (Unix, Mac OS X, or Cygwin) Run /[progname] [list of arguments] Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 32 / 47

Next Lecture Fisher s Exact Test More on C++ Programming Standard Template Library User-defined data types Hyun Min Kang Biostatistics 615/815 - Lecture 2 September 8th, 2011 33 / 47