+ All Categories
Home > Documents > 05 Applicative Programming

05 Applicative Programming

Date post: 04-Jun-2018
Category:
Upload: anidcohen9058
View: 223 times
Download: 0 times
Share this document with a friend

of 46

Transcript
  • 8/13/2019 05 Applicative Programming

    1/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    5. Applicative Programming

    1. Juli 2011

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    2/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Outline

    Recapitulation

    Computer architecture extended:Registers and caches

    Header files

    Global variables and constants

    Namespaces

    Loops revisited

    Enums

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    3/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    r e t u r n b ;}b = b%a ;

    }

    What is missing in this code?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    4/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    r e t u r n b ;}b = b%a ;

    }

    What is missing in this code? What does the return bdo?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    5/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    r e t u r n b ;}b = b%a ;

    }

    What is missing in this code? What does the return bdo?

    What happens if we replae a==0witha=0?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    6/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    i n t a ;a=b;r e t u r n b ;

    }b = b%a ;r e t u r n b ;

    }

    What value doesa have in each single code line?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    7/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    i n t a ;a=b;r e t u r n b ;

    }b = b%a ;r e t u r n b ;

    }

    What value doesa have in each single code line?

    What happens with the following snippet?

    i n t a=10;

    i n t b=20;i n t r = f o o ( a , b )

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    8/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    i n t a ;a=b;r e t u r n b ;

    }b = b%a ;r e t u r n b ;

    }

    What value doesa have in each single code line?

    What happens with the following snippet?

    i n t a=10;

    i n t b=20;i n t r = f o o ( a , b )

    Canfoo()modify the value of the variable a in our main application?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    9/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Recapitulation

    i n t foo (i n t a , i n t b ) {i f (a==0) {

    i n t a ;a=b;r e t u r n b ;

    }b = b%a ;r e t u r n b ;

    }

    What value doesa have in each single code line?

    What happens with the following snippet?

    i n t a=10;

    i n t b=20;i n t r = f o o ( a , b )

    Canfoo()modify the value of the variable a in our main application?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    10/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    5.1.Computer Architecture Excursus: Registers andCaches

    John von Neumann

    John von Neumann

    19031957 Manhattan Project (Los Alamos) June 30, 1945 (but Turing

    et. al. published similar ideas)

    Computer Consists of FourComponents

    There is aVon-Neumann bottleneck

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 5 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    11/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    The Neumann Bottleneck

    One or two documents in the office (two registers in the ALU) aint sufficient.

    Introduce more registers (Itanium e.g. has 128 of them).

    However, number of registers still is limited.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    12/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    The Neumann Bottleneck

    Running into the basement is time consuming, and

    The bigger the basement (memory), the slower the search becomes.

    The faster the processor, the more annoying the slow search in the memory is.

    Can we study this effect?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    13/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    The Memory Gap

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 41

    C t A hit t E R i t d C h H d Fil Th P il d Gl b l V i bl L E

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    14/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Idea of a Cache

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    15/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Cache Levels

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    16/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Caches

    Computers have a hierarchy of caches and lots of registers.

    The time to finish one operation depends significantly on where the data is locatedright now.

    It is important for many algorithms to exhibit spatial localityandtemporal locality.

    Brain teaser 1: How would you implement a matrix-vector product?

    Brain teaser 2: What is the fundamental challenge if we transpose a matrix?

    Brain teaser 3: What is the best way to run through a Cartesian grid?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    17/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    5.2.Header Files

    C/C++ distinguishes declaration and definition.

    Motivation for this is still missing and requires a more general definition thanbefore.

    Declaration: Tell the compiler what names are available.

    Definition: Define where the name is stored.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 12 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    18/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Functions and Their Declaration

    v o i d foo (i n t a ) {

    / / do something i n t e l l i g e n t}

    . . .f o o ( 4 4 ) ;

    This definition is both a declaration and a definition.

    A function call is replaced by a reset of the program counter (and some additionalthings).

    Sometimes, wed like to split up files into several files.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 13 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    19/46

    p g p p

    Functions and Their Declaration

    / / F i l e A

    v o i d foo (i n t a ) {/ / do something i n t e l l i g e n t

    }

    / / F i l e Bv o i d b a r (i n t a ) {

    f o o ( a + 2 ) ;}

    We can invent some clever C/C++-concatenate operation (C/C++ actually has such athing) and make file B compile.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    20/46

    p g p p

    ...However

    / / F i l e A

    v o i d foo (i n t a ) {/ / do something i n t e l l i g e n t

    }

    / / F i l e Bv o i d b a r (i n t a ) {

    f o o ( a + 2 ) ;}

    / / F i l e Cv o i d t a r (i n t a ) {

    f o o ( a + 4 ) ;}

    We can invent some clever C/C++-concatenate operation (C/C++ actually has such a

    thing) and make file B and file C compile.However, the linking process fails as the symbol foo()is defined twice.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    21/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

  • 8/13/2019 05 Applicative Programming

    22/46

    Function Definitions

    Writing the functions declaration over and over again is cumbersome and error prone.Lets put it into a file of its own. Files containing solely declarations, are called header

    filesand typically have the extension .h or .hpp.

    / / F i l e my. hv o i d foo (i n t a ) ;

    / / F i l e my. cpp# i n c l u d e my.h v o i d foo (i n t a ) {

    / / do something i n t e l l i g e n t}

    / / F i l e B# i n c l u d e my.h v o i d b a r (i n t a ) {

    f o o ( a + 2 ) ;}

    / / F i l e C# i n c l u d e my.h v o i d t a r (i n t a ) {

    f o o ( a + 4 ) ;}

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    23/46

    Header Files

    / / F i l e my. h

    v o i d foo (i n t a ) ;

    / / F i l e my. cpp# i n c l u d e my.h v o i d foo (i n t a ) {

    / / do something i n t e l l i g e n t}

    A good header file never contains definitions.

    A good header files has lots of comments as other editors of files see it frequently.

    A header file is typically accompanied by one implementation file (best practice).

    Every C/C++ and every UNIX installation comes along with tons of header files.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    24/46

    Linker Usage

    C comes along with lots of useful standard libraries.

    These libraries provide tons of useful operations.

    Seemath.hfor example or

    iostreamwhich provides the terminal output function

  • 8/13/2019 05 Applicative Programming

    25/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

  • 8/13/2019 05 Applicative Programming

    26/46

    Include Guards

    / / F i l e a . h

    # i f n d e f INCLUDE A H# d e f i n e INCLUDE A Hv o i d foo (i n t a ) ;# e n d i f

    / / F i l e b . h# i n c l u d e my.h v o i d b a r (i n t a ) ;

    / / F i l e b . cpp# i n c l u d e b . h v o i d b a r (i n t a ) {\ l d o t s }

    / / F i l e c . h# i n c l u d e my.h # i n c l u d e b . h

    v o i d t a r ( ) ;

    / / F i l e c . cpp# i n c l u d e c . h v o i d t a r ( ) {\ l d o t s }

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    27/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

  • 8/13/2019 05 Applicative Programming

    28/46

    Global Variables and Constants

    / / T h i s i s my he ad er

    # i f n d e f IDENTIFIER FOR PI HEADER H#d ef in e IDENTIFIER FOR PI HEADER H/ / A l l t h e d e c l a r a t i o n s here

    c o n s t d o ub l e PI ; / / T e l l s c om pi le r t h a t t h i s v a ri a b l e may n o t be / / changed .

    # e n d i f

    / / T h i s i s my i m p l e m e n t a t i o n# i n c l u d e p i . h c o n s t d o ub l e P I = 2 0 ;

    / / Another f i l e# i n c l u d e p i . h \ l d o t sdouble myValue ;

    \ l d o t smyValue = p i ;

    Remember difference ofdeclarationand definition.

    Other files only need the definition of the variable (which is a constant here).

    Linker then is responsible to bring together definition (address) and references tothe symbol.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 23 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    29/46

    Multiple Declarations with Different Context

    / / T h i s i s he ade r A

    # i f n d e f IDENTIFIER FOR HEADER A H#d ef in e IDENTIFIER FOR HEADER A H/ C al c u l a t es f o r c e between t wo p a r t i c l e s ( L ennard Jones)/

    f u n c t i o n double getForc e ( c o n s t d o ub l e& d i st an ce ) ;# e n d i f

    / / T h i s i s he ade r B# i f n d e f IDENTIFIER FOR HEADER B H#d ef in e IDENTIFIER FOR HEADER B H/ C a l c u l a t e s f o r c e bet ween t wo p a r t i c l e s (WCA model )/

    f u n c t i o n double getForc e ( c o n s t d o ub l e& d i st an ce ) ;# e n d i f

    Sometimes, functions and variables have the same name as they were developedby different people and written into different files.

    It is very difficult to distinguish between these names, i.e. we have to analyse the(indirect) include paths.

    Sometimes, we might want to use both functions within one implementation file.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    30/46

    Namespaces

    / / T h i s i s he ade r A

    # i f n d e f IDENTIFIER FOR HEADER A H#d ef in e IDENTIFIER FOR HEADER A Hnamespace lenn ardJo nes {

    / C al c u l a t es f o r c e between t wo p a r t i c l e s ( L ennard Jones)/

    double getForc e ( c o n s t d o ub l e& d i st an ce ) ;}

    # e n d i f

    / / T h i s i s he ade r B# i f n d e f IDENTIFIER FOR HEADER B H#d ef in e IDENTIFIER FOR HEADER B Hnamespace wca {

    / C a l c u l a t e s f o r c e bet ween t wo p a r t i c l e s (WCA model )

    /double getForc e ( c o n s t d o ub l e& d i st an ce ) ;

    }# e n d i f

    With namespaces, we can group variables, constants, and functions into logic subsets.

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    31/46

    Using Namespaces

    / / T h i s i s t h e he ad er

    # i f n d e f IDENTIFIER FOR HEADER A H#d ef in e IDENTIFIER FOR HEADER A Hnamespace lenn ardJo nes {

    / C al c u l a t es f o r c e between t wo p a r t i c l e s ( L ennard Jones)/

    double getForc e ( c o n s t d o ub l e& d i st an ce ) ;}

    # e n d i f

    / / T h i s i s t h e i m p l e m e n t a t i o n# i n c l u d e le n n a rd Jon e s . h double le n n a rd Jon e s : : g e tFo rce ( c o n s t d o ub l e& d i s ta nc e ) {\ l d o t s}

    Namespaces are predecessors of the identifier. Namespaces can be embedded into each other.

    Alternatively, you can use

    using namespace lennardJones ;

    5. Applicative ProgrammingEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    32/46

    The Standard Library

    # i n c l u d e

    i n t main ( ) {i n t a = 1 ;s t d : : c ou t

  • 8/13/2019 05 Applicative Programming

    33/46

    5.4.Loops

    Create three files: main.cpp,fibonacci.cpp, andfibonacci.h.

    Define a function voidprintFibonacci(int max)infibonacci.cpp. It shall be embeddedinto the namespacefib.

    Make printFibonacci(int max) print allFibonacci numbers

    Fn= Fn1+Fn2 F0 = 0 F1 = 1

    fromFn {0, max}.

    At leastF0 andF1 shall be printed!

    Realise the computation with a while

    loop, or do it recursively.

    5. Applicative ProgrammingEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    34/46

    Do-While Loop

    do {/ / something i n t e l l i g e n t

    } w h i l e ( e xp re ssio n ) ;

    Rewrite your code using the do-while loop.

    5. Applicative ProgrammingEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    35/46

    The For-Loop

    i n t a =0 ;

    do {/ / something i n t e l l i g e n ta ++;

    } w h i l e ( a

  • 8/13/2019 05 Applicative Programming

    36/46

    Semantics of the For Loop

    f o r (i n t a = 0 ; a

  • 8/13/2019 05 Applicative Programming

    37/46

    Scoping within a For Loop

    i n t a=20;

    f o r (i n t a = 0 ; a

  • 8/13/2019 05 Applicative Programming

    38/46

    A For Loop is a While Loop

    f o r (i n t a = 0 ; a

  • 8/13/2019 05 Applicative Programming

    39/46

    What do These Snippets Do?

    f o r (i n t a = 0 ; a

  • 8/13/2019 05 Applicative Programming

    40/46

    What do These Snippets Do?

    f o r (i n t a = 0 ; a

  • 8/13/2019 05 Applicative Programming

    41/46

    For Best Practices

    f o r (i n t a = 0 ; a

  • 8/13/2019 05 Applicative Programming

    42/46

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Enums in Action

  • 8/13/2019 05 Applicative Programming

    43/46

    Enums in Action

    enum Co lo u r {

    Red, Green , Blue} ;

    \ l d o t sCo lo u r myColou r = Red ;\ l d o t si f ( myColour==Green ) {

    . . .}

    5. Applicative ProgrammingEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 38 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Enums

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    44/46

    Enums

    enum Co lo u r {

    Red, Green , Blue} ;

    Mind the semicolon terminating the enum definition.

    Enums are basically integers (no type safety).

    Enum variants belong the enclosing namespace.

    5. Applicative ProgrammingEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 39 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Enums and Integers

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    45/46

    Enums and Integers

    enum Co lo u r {

    Red, Green , Blue} ;

    / Maps c o l o u r t o a g re y v al u e /

    i n t g e tGre yVa lu e ( co n st C ol ou r& c o l o u r ) {. . .

    }

    . . .i n t a = g e tG r ey V al u e ( 1 7 ) ;

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 40 of 41

    Computer Architecture Excursus: Registers and Caches Header Files The Precompiler and Global Variables Loops Enums

    Enums and Namespaces

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 8/13/2019 05 Applicative Programming

    46/46

    Enums and Namespaces

    namespace mySpace {

    enum Co lo u r {Red, / / i s mapped to 0 Green , / / i s mapped t o 1Blue / / i s mapped t o 2

    } ;enum TUMColours {

    Blue , / / i s mapped to 0 Orange , / / i s mapped t o 1

    White , / / i s mapped t o 2 Bla ck / / i s mapped t o 3 }

    }

    mySpace : : Colour co lo ur = mySpace : : Blue ; / / wha t h ap pe ns?

    5. Applicative Programming

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 41 of 41

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl

Recommended