3469 — Practical Numerical Simulations Mike Peardon Michaelmas Term 2015-16 School of Mathematics

advertisement
3469 — Practical Numerical Simulations
Mike Peardon
School of Mathematics
Trinity College Dublin
Michaelmas Term 2015-16
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
1/9
Introduction to C++
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
2/9
C++ is a compiled language
A very simplified version of the compilation process is:
mytest.cc
mytest
‘
g++ -o mytest mytest.cc
400800:
400803:
400806:
400809:
40080d:
400811:
mov
mov
mov
callq
add
cmp
%r13,%rdx
%r14,%rsi
%r15d,%edi
*(%r12,%rbx,8)
$0x1,%rbx
%rbp,%rbx
...
int main()
{
cout <<
"Hello World\n";
return 0;
}
...
#include <iostream>
using std::cout;
Human readable
Text file
Computer readable
Executable
In this simple example, the compiler translates text files to an
executable
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
3/9
Simplest built-in data-types
(1)
Data is stored in labelled variables, declared using the
syntax <type> <name>;. Commonly used examples for
numerical simulation are:
Declaration
used for
int x;
± integer
float y; floating point number
double z; floating point number
char c;
ASCII character
bool q;
Boolean (true/false)
size (bits)
32
32
64
8
8
Integer variables can be declared unsigned and can then
only be positive.
When a variable comes into scope, the memory to store it is
allocated automatically and can be accessed via label
given.
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
4/9
Arithmetic operators
C++ has a simple syntax for basic arithmetic:
a+b
a-b
a*b
a/b
add a to b
subtract a from b
multiply a by b
divide a by b
... also, simple short-hands to increment (add one to) and
decrement (subtract one from) an integer:
j++
j−−
increment j by one
decrement j by one
... and a simple syntax to over-write a variable:
a+=b
a-=b
a*=b
a/=b
Mike Peardon (TCD)
replace a with a+b
replace a with a-b
replace a with a*b
replace a with a/b
3469
Michaelmas Term 2015-16
5/9
Some examples: (1)
Output
Code
#include <iostream>
using std::cout;
int main()
{
double d = -44.7;
double twice_d;
}
2 x -44.7=-89.4
twice_d = 2.0 * d;
cout << "2 x " << d <<
"=" << twice_d << "\n";
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
6/9
Some examples: (2)
Output
Code
#include <iostream>
using std::cout;
int main()
{
int i=10,j=20;
i *= 2*j-i;
i--;
cout << "i=" << i << "\n";
}
i=299
q=1106.3
float q=3.7*float(i);
cout << "q=" << q << "\n";
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
7/9
Functions
Instructions can be collected into functions, which have a
common syntax:
<return type> <fn name>(<arguments>){ ... }
The return type can be declared void, if the function returns
nothing.
Functions can declare variables, visible only local to that
function
Functions can be called recursively
Functions can take arguments or not. A function is called
using its name
C++ follow the call-by-value syntax, meaning a copy of any
variable is made and the copy is given to the function
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
8/9
Functions are call-by-value
#include <iostream>
using std::cout;
int func(int x)
{
x += 10;
return x*x;
}
int main()
{
int z=-3;
int q=func(z);
cout << "q=" << q << ",z=" << z << "\n";
}
Code outputs q=49,z=-3 - note the value of z is unchanged.
Mike Peardon (TCD)
3469
Michaelmas Term 2015-16
9/9
Download