Introduction to C++
Organisation
The course will always take place in the Econometrics Room, MSE, at the following times Thursday 7 October 9.30am - 12.30pm (3h) Thursday 14 October 9am - 12pm (3h) Thursday 21 October 9am - 12pm (3h) Thursday 28 October 9am - 12pm (3h) 2 more classes: TBA (23h) Final mark is roughly based on Attendance - 20% Participation in classes and practicals - 30% Final Project / Exam - 50%
Introduction to C++
Google EPI C++ or EPI Paris 1 C++ General questions can be sent to: kimiya.minoukadeh@gmail.com
Introduction to C++
C++ references
A very useful site: C++ Language Tutorial: http://www.cplusplus.com/doc/tutorial/ An easy-to-read book with many examples: C++ How to Program by Deitel & Deitel
Introduction to C++
Contents
Why C++?
Random numbers
Introduction to C++
In a high-level language such as MATLAB, code is often easy to write and contains few lines Code in MATLAB a = 1:10; b = The sum of integers from 1 to 10 is; disp(b); sum(a)
Output The sum of integers from 1 to 10 is 55
Introduction to C++
So why C++?
In a C++ code, we must specify the type of variables used (int, char,...) determine the maximum number of elements in list (array) put the core of the code in a function called main... For even a simple task, C++ code is often long. So why choose C++ over MATLAB or other programming languages?
Introduction to C++
Programming Languages
1
Introduction to C++
C++ in Finance
Introduction to C++
C++ Compilers
In order to run C++ code, it will rst need to be compiled using a C++ compiler. Unix-like/Linux systems
GCC (GNU C compiler) for C++, called g++.
Windows
IDE (Integrated Development Environment) such as Microsoft Visual C++ (license needed) or DevC++ (free).
Mac OS X
IDE such as Xcode (normally already installed).
Introduction to C++
Why C++?
Random numbers
Introduction to C++
First program
helloworld.cpp 1. #include <iostream> 2. using namespace std; 3. 4. int main() 5. { 6. cout << "Hello World!" << endl; 7. return 0; 8. }
1. Directive to preprocessor: include the header le iostream 4. The funtion main() contains the main body of the program 6. std::cout is the standard ouput stream 6. std::endl inserts a new-line character
Kimiya Minoukadeh kimiya.minoukadeh@gmail.com Introduction to C++
Using variables
Data may also be stored in variables. A variable must have an identier to distinguish it from others
no variable may have the same name as another a valid identier is a sequence of one or more letters, digits or underscore characters ( )
Introduction to C++
Data Types
Declaring variables int a; // declaring an integer int i = 1; // declaring + initializing integer a = 3.2; // Beware! a = floor(3.2)
char c = h; char st1[] = "Hello World!"; // array of (13+1) chars char st2[30]; // if to be initialized later
Introduction to C++
Operators
Assignment operator (=): int a, b; a = 5; b = 10; Arithmetic operators ( +, -, *, /, % ): a = a*b; a = b%2; Compound operators (+=, -=, *=, /=, %=)
a a a a += b; -= 5; /= b; *= b; a a a a = = = = a a a a + b; - 5; / b; * b;
Introduction to C++
More operators
Relational and equality operators ( ==, !=, >, <, >=, <=). The result of a relational operation is a Boolean value (true or false) (7 == 5) // evaluates to false. (5 > 4) // evaluates to true. ((a*b) != 2) // ... The assignment operator (=) and the equality operator (==) should not be confused! Logical operators ( !, &&, || ). The operator ! is the C++ operator to perform the Boolean operation NOT. !false !(5 == 5) (0 <= 4 || 10 < 2) // evaluates to true // evaluates to false // evaluates to true
Introduction to C++
Conditional structures
The if/else statement if (condition) { statements1 } else { statements2 } The two may be concatenated to test a range of values code extract if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0"; The braces {} must be added when there is more than one statement per if/else clause.
Kimiya Minoukadeh kimiya.minoukadeh@gmail.com Introduction to C++
Iteration structures
The for loop for (initialization; condition; increase) {statements}
code extract int N = 10; int array[N]; for (int i=0; i<N; i++) { array[i] = i*i; cout << array[i] << " "; } cout << endl; What is the output?
Introduction to C++
Iteration structures
The while loop while (expression) {statements} code extract int val = 6; int guess; bool found = false; cout << "Guess a number between 1 and 10: "; while (!found) { cin >> guess; if (guess != val) cout << endl << "Try again: "; else found = true; } cout << "Well done! The number was " << val << endl;
Kimiya Minoukadeh kimiya.minoukadeh@gmail.com Introduction to C++
Practical Assignment
Introduction to C++
Header les
The C++ standard library provides many functionalities, whose prototypes are given in various header les. The header les provide function prototypes and various data types and constants used by the library. Some examples are.. Header le <iostream> <cctype> <cmath> <ctime> <fstream> Description Standard input/output functions Functions for testing characters Math library functions Manipulating the time and date Input/ouput to les on disk Examples cout, cin islower() exp(), pow() time() open(), close()
There are many more.. check before writing your own functions.
Introduction to C++
Why C++?
Random numbers
Introduction to C++
Introduction to C++
So, if the nth bit in the sequence has value bn {0, 1}, then
N2
x=
n=0
2n bn
2N1 bN1
Introduction to C++
What would be result of x+1? What is the binary representation of -1? Use unsigned integers or double for a wider range of numbers.
Introduction to C++
Why C++?
Random numbers
Introduction to C++
Random Numbers
Include header le <cstdlib> To generate a random number use function rand() The number is drawn uniformly between 0 and RAND MAX In fact, these random numbers are generated by a pseudorandom number generator: an algorithm for generating a sequence of numbers that approximates the properties of random numbers. Algorithm Given a, b, m > 0 and a seed x0 , the recurrence xn+1 = (a xn + b) mod m is used to generate the sequence of random numbers.
Introduction to C++
Introduction to C++
Repeating sequence
With the random number generator, the sequence eventually repeats itself. For long simulations (e.g. Monte Carlo) where true randomness is important, one can use a larger modulus m. C++ provides a function drand48() where m = 248 . Mersenne twister (1997) is another random number generator, where m = 219937 . The Mersenne Twister package is freely available for C++.
Introduction to C++
The C++ code is given in boxmuller.cpp on the course website. It will be needed for Exercise 7 of the practical assignment.
Introduction to C++
Practical Assignment 1
Introduction to C++