course for computational sciences and engineering at d ... · course for computational sciences and...

99
Computer Science Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerhoff AS 2018 1

Upload: others

Post on 20-Jun-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer Science

Course for Computational Sciences and Engineeringat D-MATH of ETH Zurich

Felix Friedrich, Malte Schwerhoff

AS 2018

1

Page 2: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

1. Introduction

Computer Science: Definition and History, Algorithms, TuringMachine, Higher Level Programming Languages, Tools, The firstC++Program and its Syntactic and Semantic Ingredients

21

Page 3: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

What is Computer Science?

The science of systematic processing of informations,. . .. . . particularly the automatic processing using digital computers.

(Wikipedia, according to “Duden Informatik”)

22

Page 4: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

What is Computer Science?

The science of systematic processing of informations,. . .

. . . particularly the automatic processing using digital computers.

(Wikipedia, according to “Duden Informatik”)

22

Page 5: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

What is Computer Science?

The science of systematic processing of informations,. . .. . . particularly the automatic processing using digital computers.

(Wikipedia, according to “Duden Informatik”)

22

Page 6: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer Science vs. Computers

Computer science is not about machines, in the same waythat astronomy is not about telescopes.

Mike Fellows, US Computer Scientist (1991)

23

Page 7: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer Science vs. Computers

Computer science is also concerned with the development of fastcomputers and networks. . .

. . . but not as an end in itself but for the systematic processingof informations.

24

Page 8: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer Science vs. Computers

Computer science is also concerned with the development of fastcomputers and networks. . .. . . but not as an end in itself but for the systematic processingof informations.

24

Page 9: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer Science 6= Computer Literacy

Computer literacy: user knowledge

Handling a computerWorking with computer programs for text processing, email,presentations . . .

25

Page 10: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer Science 6= Computer Literacy

Computer Science Fundamental knowledge

How does a computer work?How do you write a computer program?

25

Page 11: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

This course

Systematic problem solving with algorithms and the programminglanguage C++.Hence:

not onlybut also programming course.

26

Page 12: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Algorithm: Fundamental Notion of Computer Science

Algorithm:

Instructions to solve a problem step by step

Execution does not require any intelligence, but precision (evencomputers can do it)according to Muhammed al-Chwarizmi,author of an arabiccomputation textbook (about 825)

“Dixit algorizmi. . . ” (Latin translation)

27

Page 13: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Algorithm: Fundamental Notion of Computer Science

Algorithm:

Instructions to solve a problem step by stepExecution does not require any intelligence, but precision (evencomputers can do it)

according to Muhammed al-Chwarizmi,author of an arabiccomputation textbook (about 825)

“Dixit algorizmi. . . ” (Latin translation)

27

Page 14: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Algorithm: Fundamental Notion of Computer Science

Algorithm:

Instructions to solve a problem step by stepExecution does not require any intelligence, but precision (evencomputers can do it)according to Muhammed al-Chwarizmi,author of an arabiccomputation textbook (about 825)

“Dixit algorizmi. . . ” (Latin translation)27

Page 15: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Oldest Nontrivial AlgorithmEuclidean algorithm (from the elements from Euklid, 3. century B.C.)

Input: integers a > 0, b > 0

Output: gcd of a und b

While b 6= 0If a > b then

a← a− belse:

b← b− aResult: a.

a b

a b a b a b

28

Page 16: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Oldest Nontrivial AlgorithmEuclidean algorithm (from the elements from Euklid, 3. century B.C.)

Input: integers a > 0, b > 0

Output: gcd of a und b

While b 6= 0If a > b then

a← a− belse:

b← b− aResult: a.

a b a b

a b a b

28

Page 17: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Oldest Nontrivial AlgorithmEuclidean algorithm (from the elements from Euklid, 3. century B.C.)

Input: integers a > 0, b > 0

Output: gcd of a und b

While b 6= 0If a > b then

a← a− belse:

b← b− aResult: a.

a b a b a b

a b

28

Page 18: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Oldest Nontrivial AlgorithmEuclidean algorithm (from the elements from Euklid, 3. century B.C.)

Input: integers a > 0, b > 0

Output: gcd of a und b

While b 6= 0If a > b then

a← a− belse:

b← b− aResult: a.

a b a b a b a b28

Page 19: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Live Demo: Turing Machine

29

Page 20: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0 1 2 3 4 5 6 7 8 9

Speicher

Links Rechts

Register30

Page 21: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8 9

Speicher

Programmcode

Links Rechts

Register30

Page 22: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Programmcode Daten

Links Rechts

Register30

Page 23: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Programmcode Daten

Links Rechts

Register

Daten

30

Page 24: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Links

b

Rechts

a

Register

While b 6= 0If a > b then

a← a− belse:

b← b− aErgebnis: a.

30

Page 25: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Links

b

Rechts

a

Register

While b 6= 0If a > b then

a← a− belse:

b← b− aErgebnis: a.

30

Page 26: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Links

b

Rechts

a

Register

While b 6= 0If a > b then

a← a− belse:

b← b− aErgebnis: a.

30

Page 27: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Links

b

Rechts

a

Register

While b 6= 0If a > b then

a← a− belse:

b← b− aErgebnis: a.

30

Page 28: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Links

b

Rechts

a

Register

While b 6= 0If a > b then

a← a− belse:

b← b− aErgebnis: a.

30

Page 29: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Euklid in the Box

0

[8]→ L

1

[9]→ R

2

L = 0?stop

3

R > L?springe

zu 6

4

L − R→ [8]

5

springezu 0

6

R − L→ [9]

7

springezu 0

8

b

9

a

Speicher

Links Rechts

Register

While b 6= 0If a > b then

a← a− belse:

b← b− aErgebnis: a.

30

Page 30: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computers – Concept

A bright idea: universal Turing machine (Alan Turing, 1936)

Alan Turing31

Page 31: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computer – Implementation

Z1 – Konrad Zuse (1938)ENIAC – John Von Neumann (1945)

Konrad Zuse

John von Neumann32

Page 32: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Memory for data and program

Sequence of bits from {0, 1}.Program state: value of all bits.Aggregation of bits to memory cells (often: 8 Bits = 1 Byte)

34

Page 33: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Memory for data and program

Every memory cell has an address.Random access: access time to the memory cell is (nearly)independent of its address.

34

Page 34: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computing speed

In the time, on average, that the sound takes to travel from from mymouth to you ...

30 m

a contemporary desktop PC can process more than 100 millionsinstructions 1

1Uniprocessor computer at 1 GHz.35

Page 35: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computing speed

In the time, on average, that the sound takes to travel from from mymouth to you ...

30 m

a contemporary desktop PC can process more than 100

millionsinstructions 1

1Uniprocessor computer at 1 GHz.35

Page 36: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Computing speed

In the time, on average, that the sound takes to travel from from mymouth to you ...

30 m =̂ more than 100.000.000 instructions

a contemporary desktop PC can process more than 100 millionsinstructions 1

1Uniprocessor computer at 1 GHz.35

Page 37: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Programming

With a programming language we issue commands to a computersuch that it does exactly what we want.The sequence of instructions is the(computer) program

The Harvard Computers, human computers, ca.189036

Page 38: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why programming?

Do I study computer science or what ...

There are programs for everything ...I am not interested in programming ...because computer science is a mandatory subject here,unfortunately.... . .

37

Page 39: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why programming?

Do I study computer science or what ...There are programs for everything ...

I am not interested in programming ...because computer science is a mandatory subject here,unfortunately.... . .

37

Page 40: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why programming?

Do I study computer science or what ...There are programs for everything ...I am not interested in programming ...

because computer science is a mandatory subject here,unfortunately.... . .

37

Page 41: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why programming?

Do I study computer science or what ...There are programs for everything ...I am not interested in programming ...because computer science is a mandatory subject here,unfortunately...

. . .

37

Page 42: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why programming?

Do I study computer science or what ...There are programs for everything ...I am not interested in programming ...because computer science is a mandatory subject here,unfortunately.... . .

37

Page 43: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Mathematics used to be the lingua franca of the natural sci-ences on all universities. Today this is computer science.Lino Guzzella, president of ETH Zurich, NZZ Online, 1.9.2017

((BTW: Lino Guzzella is not a computer scientist, he is a mechanical engineer and prof. for thermotronics )

38

Page 44: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

This is why programming!

Any understanding of modern technology requires knowledgeabout the fundamental operating principles of a computer.Programming (with the computer as a tool) is evolving a culturaltechnique like reading and writing (using the tools paper andpencil)

Programming is the interface between engineering and computerscience – the interdisciplinary area is growing constantly.Programming is fun (and is useful)!

39

Page 45: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

This is why programming!

Any understanding of modern technology requires knowledgeabout the fundamental operating principles of a computer.Programming (with the computer as a tool) is evolving a culturaltechnique like reading and writing (using the tools paper andpencil)Programming is the interface between engineering and computerscience – the interdisciplinary area is growing constantly.

Programming is fun (and is useful)!

39

Page 46: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

This is why programming!

Any understanding of modern technology requires knowledgeabout the fundamental operating principles of a computer.Programming (with the computer as a tool) is evolving a culturaltechnique like reading and writing (using the tools paper andpencil)Programming is the interface between engineering and computerscience – the interdisciplinary area is growing constantly.Programming is fun (and is useful)!

39

Page 47: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Programming Languages

The language that the computer can understand (machinelanguage) is very primitive.Simple operations have to be subdivided into (extremely) manysingle stepsThe machine language varies between computers.

40

Page 48: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Higher Programming Languages

can be represented as program text that

can be understood by humansis independent of the computer model→ Abstraction!

41

Page 49: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why C++?

Other popular programming languages: Java, C#, Objective-C,Oberon, Javascript, Go, Python, . . .

General consensus:

„The” programming language for systems programming: CC has a fundamental weakness: missing (type) safety

42

Page 50: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why C++?

Other popular programming languages: Java, C#, Objective-C,Oberon, Javascript, Go, Python, . . .

General consensus:

„The” programming language for systems programming: CC has a fundamental weakness: missing (type) safety

42

Page 51: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Why C++?

Over the years, C++’s greatest strength and its greatestweakness has been its C-Compatibility – B. Stroustrup

43

Page 52: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Deutsch vs. C++

DeutschEs ist nicht genug zu wissen,man muss auch anwenden.(Johann Wolfgang von Goethe)

C++

// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4

44

Page 53: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Syntax and Semantics

Like our language, programs have to be formed according tocertain rules.

Syntax: Connection rules for elementary symbols (characters)Semantics: interpretation rules for connected symbols.

45

Page 54: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Syntax and Semantics

Corresponding rules for a computer program are simpler but alsomore strict because computers are relatively stupid.

45

Page 55: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Syntax and Semantics of C++

Syntax

When is a text a C++ program?

I.e. is it grammatically correct?

Semantics

What does a program mean?

Which algorithm does a program implement?

47

Page 56: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Programming Tools

Editor: Program to modify, edit and store C++program textsCompiler: program to translate a program text into machinelanguage

49

Page 57: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Programming Tools

Computer: machine to execute machine language programsOperating System: program to organize all procedures such asfile handling, editor-, compiler- and program execution.

49

Page 58: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

The first C++ program// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}51

Page 59: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Most important ingredients. . . Statements// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

Do something (read in a)!

51

Page 60: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Most important ingredients. . . Expressions// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

Compute a value (a2)!

51

Page 61: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

“Accessories:” Comments// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}53

Page 62: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

“Accessories:” Comments// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

comments

53

Page 63: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Comments and Layout

The compiler does not care...

#include <iostream>int main(){std::cout << "Compute a^8 for a =? ";int a; std::cin >> a; int b = a * a; b = b * b;std::cout << a << "^8 = " << b*b << "\n";return 0;}

... but we do!

55

Page 64: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Comments and Layout

The compiler does not care...

#include <iostream>int main(){std::cout << "Compute a^8 for a =? ";int a; std::cin >> a; int b = a * a; b = b * b;std::cout << a << "^8 = " << b*b << "\n";return 0;}

... but we do!

55

Page 65: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

“Accessories:” Include and Main Function// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}56

Page 66: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

“Accessories:” Include and Main Function// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

include directive

56

Page 67: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

“Accessories:” Include and Main Function// Program: power8.cpp// Raise a number to the eighth power.#include <iostream>int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

declaration of the main function

56

Page 68: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Statements: Do something!int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

59

Page 69: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Statements: Do something!int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

expression statements

59

Page 70: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Statements: Do something!int main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}return statement

59

Page 71: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Statements – Effectsint main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

effect: output of the string Compute ...

Effect: input of a number stored in a

Effect: saving the computed value of a*a into b

Effect: saving the computed value of b*b into b

Effect: output of the value of a and the computed value of b*bEffect: return the value 0

63

Page 72: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Statements – Variable Definitionsint main() {

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b ∗ b << "\n";return 0;

}

declaration statement

typenames

65

Page 73: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Literals

represent constant valueshave a fixed type and valueare "syntactical values"

Examples:

0 has type int, value 0.

1.2e5 has type double, value 1.2 · 105.

69

Page 74: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Variables

represent (varying) valueshave

nametypevalueaddress

Exampleint a; defines a variable with

name: a

type: int

value: (initially) undefined

Address: determined bycompiler (and linker, runtime)

70

Page 75: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Variables

represent (varying) valueshave

nametypevalueaddress

Exampleint a; defines a variable with

name: a

type: int

value: (initially) undefined

Address: determined bycompiler (and linker, runtime)

70

Page 76: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions: compute a value!

represent Computations

are either primary (b)

or composed (b*b). . .

. . . from different expressions, using operators

have a type and a value

Analogy: building blocks

73

Page 77: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions: compute a value!

represent Computations

are either primary (b)

or composed (b*b). . .

. . . from different expressions, using operators

have a type and a value

Analogy: building blocks

73

Page 78: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions: compute a value!

represent Computations

are either primary (b)

or composed (b*b). . .

. . . from different expressions, using operators

have a type and a value

Analogy: building blocks

73

Page 79: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions: compute a value!

represent Computations

are either primary (b)

or composed (b*b). . .

. . . from different expressions, using operators

have a type and a value

Analogy: building blocks

73

Page 80: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions: compute a value!

represent Computations

are either primary (b)

or composed (b*b). . .

. . . from different expressions, using operators

have a type and a value

Analogy: building blocks

73

Page 81: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions: compute a value!

represent Computations

are either primary (b)

or composed (b*b). . .

. . . from different expressions, using operators

have a type and a value

Analogy: building blocks

73

Page 82: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";

return 0;

74

Page 83: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";

return 0;

variable name, primary expression (+ name and address)

variable name, primary expression (+ name and address)

literal, primary expression74

Page 84: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions Building Blocks

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";

return 0;

composite expression

composite expression

74

Page 85: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Expressions Building Blocks

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";

return 0;

Two times composed expression

Four times composed expression74

Page 86: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";return 0;

77

Page 87: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";return 0;

L-value (expression + address)

L-value (expression + address)

R-Value (expression that is not an L-value)

77

Page 88: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b * b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a<< "^8 = " << b * b << ".\ n";return 0;

R-Value

R-Value

77

Page 89: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values

L-Wert (“Left of the assignment operator”)

Expression with address

Value is the content at the memory location according to thetype of the expression.

L-Value can change its value (e.g. via assignment)

Example: variable name

78

Page 90: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values

L-Wert (“Left of the assignment operator”)

Expression with address

Value is the content at the memory location according to thetype of the expression.

L-Value can change its value (e.g. via assignment)

Example: variable name

78

Page 91: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values

R-Wert (“Right of the assignment operator”)

Expression that is no L-value

Example: literal 0

Any L-Value can be used as R-Value (but not the other wayround)

An R-Value cannot change its value

79

Page 92: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values

R-Wert (“Right of the assignment operator”)

Expression that is no L-value

Example: literal 0

Any L-Value can be used as R-Value (but not the other wayround)

An R-Value cannot change its value

79

Page 93: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values

R-Wert (“Right of the assignment operator”)

Expression that is no L-value

Example: literal 0

Any L-Value can be used as R-Value (but not the other wayround)

Every E-Bike can be used as normal bike, but not the other wayround

An R-Value cannot change its value

79

Page 94: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Values and R-Values

R-Wert (“Right of the assignment operator”)

Expression that is no L-value

Example: literal 0

Any L-Value can be used as R-Value (but not the other wayround)

An R-Value cannot change its value

79

Page 95: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

L-Value and R-Value

80

Page 96: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Operators and Operands Building Blocks

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b * b << "\n";return 0;

81

Page 97: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Operators and Operands Building Blocks

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b * b << "\n";return 0;

left operand (output stream)right operand (string)output operator

81

Page 98: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Operators and Operands Building Blocks

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b * b << "\n";return 0;

left operand (input stream)

right operand (variable name)input operator

81

Page 99: Course for Computational Sciences and Engineering at D ... · Course for Computational Sciences and Engineering at D-MATH of ETH Zurich Felix Friedrich, Malte Schwerho AS 2018 1

Operators and Operands Building Blocks

// inputstd::cout << "Compute a^8 for a =? ";int a;std::cin >> a;

// computationint b = a ∗ a; // b = a^2b = b ∗ b; // b = a^4

// output b ∗ b, i.e., a^8std::cout << a << "^8 = " << b * b << "\n";return 0;

assignment operator

multiplication operator

81