- 62 Views
- Uploaded on
- Presentation posted in: General

Chapter 2.01

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Chapter 2.01

C++

An Introduction

Control Unit & ALU : Integrated Circuits

Central memory : Integrated Circuits

Diversified computers:

Low cost Personal Workstations

Multi-processor supercomputers

Embedded systems

Networking and distributed processing

Software :

Graphical User-interfaces

Application oriented programming.

Object oriented design & programming.

1971-1973:

Dennis Ritchie and Brian Kernighan design the C language to port the UNIX operating system from a PDP7 to a PDP11

1985 :

Bjarne Stroustrup creates a new version of C taking into account sound rules of software engineering and enabling the object oriented programming style. He calls it C++

#include <iostream>

void main() // This program computes the area of a circle

{

double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

Upper- and Lower-case letters

a b c d e ... A B C D E ...

Digits

0 1 2 3 4 5 6 7 8 9

Special signs

; , . | + - * / = > < ( ) [ ] { }

Composed signs

/* */ // == != >= <=

Reserved Words

void double const do while

Source A

Source B

Source C

Source D

Compiler X

Compiler Y

Assembler

Reloc. A

Reloc. B

Reloc. C

Reloc. D

LINKER

Object Code A+B+C+D

Role of a Linker

#include <iostream>

void main() // This program computes the area of a circle

{

double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

Comments are parts of a program text that are not taken into consideration by compilers and interpreters. They are used to facilitate understanding of the program by human readers or to give general information such as the name of the author.

Comments in C:

/* This program computes the area of a circle */

Comments in C++:

/* C style comments are used for comments that expand over several lines */

// Single line comments are also authorized.

voidmain()

{

...

}

Every C or C++ program must contain a function called main

Execution starts with that function.

A normal function has arguments and computes a value.

In this example the function main has no arguments()and computes no value ’void’.

The entire code of the function is surrounded by { and } and constitutes a “block”

PROGRAM

=

Description of data

+

Actions to perform upon these data

#include <iostream>

void main() // This program computes the area of a circle

{

double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

radius: dat 8 ;double variable

pi: equ 3.1416 ;double constant

area: dat 8 ;double variable

Constants

(Name)

Value

The value of a

constant belongs

to a type.

Variables

Name

Type

Type =

set of all values

the variable can have

const double pi = 3.1416;

double radius, area;

The type determines the internal representation of data

as well as the operations that can be performed on the data

Literal Constants

Only a value:

Named Constants

A name and a value:

The value can be a constant expression:

3.1416

"The area of this circle is "

double pi = 3.1416;

double twopi = pi * 2.0;

area := 6 * length*length;

...

tax := (price * 6) / 100;

area := 21 * length*length;

...

tax := (price * 21) / 100;

const int VAT = 6;

...

area := 6 * length*length;

...

tax := (price * VAT) / 100;

const int VAT = 21;

...

area := 6 * length*length;

...

tax := (price * VAT) / 100;

Simple Types: valuescan’t be decomposed

Integer numbers : short int, int, long int, unsigned int

Real numbers: float, double

Characters : char

Enumerated values : enum

Boolean values (true,false) : boolean

Addresses in data memory : pointer

Structured Types: Values have several components

All components have same type : array

Components can have # types : struct

More elaborated structures (only C++) : class

two’s complement integer numbers

Number of bits:

implementation dependant

In most modern computers: 32 or 64 bits

In embedded systems, often 16 bits (-32678 <= x < 32768)

Operations:

Addition +

Substraction –

Multiplication *

Integer division /5 / 2 gives 2

Modulo (remainder)%5 % 2 gives 1

Relational operators==,!=,>=,<=,>,<

Floating point real numbers

Number of bits:

implementation dependant

In almost all modern computers: IEEE754, 64 bit format

Operations:

Addition +

Substraction –

Multiplication *

Real division /5.0 / 2.0 gives 2.5

Relational operators==,!=,>=,<=,>,<

Arithmetic expressions in C and C++ are evaluated according to the traditional precedence rules of arithmetic, from left to right, in a lazy way

(x+z)*(x-y)*(x+z/(x-y))

When integers and reals are mixed in an arithmetic expression, the resulting value belongs to the real type

The value of 5.0 / 2 is 2.5

Quite often the same variable appears on the left and on the right side of an assignment.To reduce the typing effort and to help somewhat the compilation process, a shorthand that avoids typing twice the name of the variable has been introduced:

x = x+1;can be written asx++; or ++x;

x = x–1;x--; or--x;

x = x+y;x =+ y; or x += y;

For the precise interpretation of these operators, the reader should consult a reference book on C++ .

Values are defined by the programmer

enum day {mon,tue,wed,thu,fri,sat,sun};

day today;

Enumerated types are represented by integers:

By default: {0,1,2,3,…}

Programmer can specify his/her own representations

{mon=1,tue=2,wed=3,thu=4,fri=5,sat=-6,sun=-7}

Operations:

Relational operators==,!=,>=,<=,>,<

Enumerated value can be assigned to integer variable

Integer value can not be assigned enumerated variable

Main benefit : clarity

enum sex {male,female};

sex sexa,sexb;

if (sexa == male) sexb = female

else sexb = male;

Much more clear than encoding by programmer:

int sexa, sexb;

// male encoded by 0, female by 1

sexb = 1 – sexa;

Possible values:

TRUE represented by any non zero value

FALSE represented by a zero value

Operators : not! , and&& , or||

X && Y

X || Y

Y = TRUE

Y = TRUE

Y = FALSE

Y = FALSE

X = TRUE

X = TRUE

TRUE

TRUE

FALSE

TRUE

X = FALSE

X = FALSE

FALSE

TRUE

FALSE

FALSE

X = TRUE

X = FALSE

! X

FALSE

TRUE

a == bTRUE when a equals b

a != bTRUE when a different from b

a > b TRUE when a greater than b

a < bTRUE when a less than b

a >= bTRUE when a greater or equal b

a <= bTRUE when a less or equal b

have a boolean value

if((-1.0<=x)&&(x<=+1.0)) y = asin(x)

#include <iostream>

void main() // This program computes the area of a circle

{

double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

A program is a set of nested blocks

{ ...

{ ...

{ ...

} // RED

{ ... } // GREEN

} // PURPLE

} // BLUE

An identifier can be used everywhere within the block where it is declared

{ int a

{ float b

{ char c

... a =...; b =...; c =...; ... }

X

{ ... a =...; b =...; c =...; ... }

X

... a =...; b =...; c =...; ... }

X

X

... a =...; b =...; c =...; ... }

A global identifier can be redefined The local definition dominates

{ int a

{ float b

{ char a

... a = ‘$’; b =...; ... }

X

{ ... a = 2; b =...; c =...; ... }

X

... a =...; b =...; c =...; ... }

X

X

... a = 5; b =...; c =...; ... }

#include <iostream>

void main() // This program computes the area of a circle

{ double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

PROGRAM

=

Description of data

+

Actions to perform upon these data

#include <iostream>

void main() // This program computes the area of a circle

{ double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

Cout << constant string;

Cout << integer, character or double variable;

// \n in a string means “new line”

#include <iostream>

void main() // This program computes the area of a circle

{ double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

cin >> integer, character or double variable;

#include <iostream>

void main() // This program computes the area of a circle

{ double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

pihas a constant value (=3.1416)

The value ofradiushas been entered throughcin

The expression pi * radius * radius is evaluated and its value assigned to area

#include <iostream>

void main() // This program computes the area of a circle

{ double radius;

const double pi = 3.1416;

cout << "Hello, I compute the area of a circle for you \n";

cout << "I will stop if you enter a value of 0. \n";

do

{ double area;

cout << "Give me the radius in meters ? ";

cin >> radius;

area = pi * radius * radius;

cout << " The area is : " ;

cout << area;

cout << " square meters \n";

}

while (radius != 0);

}

{…}

Radius != 0

FALSE

Selection statements

boolean selector : if statement

Integer value selector : switch statement

Iteration statements

Initial termination test : while do loop

Final termination test : do while loop

Known number of iterations : for loop

Function call : See chapter 2.2

Unstructured control statements

Leave structured control statement : break

Skip an iteration: continue

...

B

B

FALSE

TRUE

FALSE

TRUE

S2

S1

S2

S1

if(x%2 == 0) cout << “pair”;

elsecout << “impair”;

B

B

FALSE

TRUE

FALSE

TRUE

S

S

// absolute value of x

if(x < 0) x = -x;

e == a

Yes

S1

No

e == b

Yes

No

S2

e == c

Yes

No

S3

S4

switch (e)

The value of the expression ecan belong to any of the types:

int,enum,char.

switch (e)

switch(today){case mon:cout<<“get up”;case tue:cout<<“get up”; case wed:cout<<“try to get up”;case thu:cout<<“get up”;case fri:cout<<“get up”;case sat:cout<<“try to get up”;case sun:cout<<“sleep”;}

A stupid example

e == a

Yes

S1;break;

No

e == b

Yes

S2;break;

No

e == c

Yes

S3;break;

No

S4

switch (e), with break

break

Any structured control instruction

break

switch (e)

switch(today){case mon:cout<<“get up”;break;case tue:cout<<“get up”;break; case wed:cout<<“try to get up”;break;case thu:cout<<“get up”;break;case fri:cout<<“get up”;break;case sat:cout<<“try to get up”;break;case sun:cout<<“sleep”;}

A slightly less stupid example

B

B

FALSE

S

TRUE

S

while(status != endoftape)

{PlaySong}

Specifications :

Given two cardinal numbers x and y

Compute g, the gcd of x and y

Algorithm :

gcd(x, y) = gcd(x, y-x)(x < y)

gcd(x, y) = gcd(x-y, y)(x > y)

while x # y

x > y

TRUE

x = x-y

y = y-x

Top level design :

Read value of x and y;

Compute g, the gcd of x and y;

Write the value of g;

#include<iostream>

void main()

{ intx,y,g;

// Read value of x and y

...

// Compute g, the gcd of x and y

...

// Write value of x,y and g

...

cin >> x; // for keeping the window

}

#include <iostream>

void main()

{int x,y,g;

// Read value of x and y

cout << " Enter two positive integers ";

cin >> x >> y;

cout << "Thank you, you entered "

cout << x << " and " << y << "\n";

// Compute g, the gcd of x and y

…

// Write value of x,y and g

…

cin >> x;

}

#include <iostream>

void main()

{int x,y,g;

// Read value of x and y

...

// Compute g, the gcd of x and y

while (x != y)

if (x > y) x = x - y;

else y = y - x;

g = x;

// Write value of x,y and g

...

cin >> x;

}

#include <iostream>

void main()

{int x,y,g;

// Read value of x and y

...

// Compute g, the gcd of x and y

...

// Write value of x,y and g

cout << "the gcd of " <<x <<" and " <<y

cout <<" is " <<g <<"\n";

cin >> x;

}

Program states for 123 and 456 as inputs

Executed instr. x y g

Cin >>x >>y123456 ?

y = y – x123333 ?

y = y – x123210 ?

y = y – x123 87 ?

x = x – y 36 87 ?

y = y – x 36 51 ?

y = y – x 36 15 ?

x = x – y 21 15 ?

x = x – y 6 15 ?

y = y – x 6 9 ?

y = y – x 6 3 ?

x = x – y 3 3 ?

g = x 3 3 3

S

S

B

TRUE

B

FALSE

do

{RemoveBolt}

while (BoltsLeft != 0)

init

cond

FALSE

TRUE

S

change

for (init,cond,change)S;

Scope of variables declared here is the entire for loop

for (int nbolt=1;nbolt <= 4;nbolt++)

{Remove bolt number nbolt}

Top level design :

Read value of n;

Compute fact = n!;

Write the value of fact;

#include<iostream>

void main()

{ intfact,n;

// Read value of n

...

// Compute fact = n!

...

// Write value of n and fact

...

cin >> n;

}

#include <iostream>

void main()

{ int n, fact = 1;

// Read value of n

...

// Compute fact = n!

for (int i = 1; i <= n; i++)

fact = fact * i;

// Write value of n and fact

...

}

Program states for n = 5

Executed instr.nfact i

cin >>n 5 1

i = 15 1 1

fact = fact * i5 1 1

i++ 5 1 2

fact = fact * i5 2 2

i++ 5 2 3

fact = fact * i5 6 3

i++ 5 6 4

fact = fact * i5 24 4

i++ 5 24 5

fact = fact * i5 120 5

The value of e is given by the infinite series:

The error when limiting the calculation to term i:

e = 1 + + + + + +

2

1

1

1

1

1

1

1

1

m!

m!

n!

3!

1!

3!

2!

2!

1!

e – ( 1 + + + + + ) <

Mathematics :

Top level design:

Read value of limits

Upper limit of error : eps

Maximum number of terms : nmax

Compute e

Number of terms needed : nterms

Write the results

Value of e

Number of terms used : nterms

#include <iostream>

void main()

{

int nterms = 0; // number of terms in series

int nmax; // maximum value for nterms

double e = 1.0; // value of the Euler number

double eps; // max tolerable error

// get values of eps and nmax

...

// compute the value of e

...

// write the value of e and nterms

...

cin >> nmax; // to keep the window open !

}

#include <iostream>

void main()

{

int nterms = 0; // number of terms in series

int nmax; // maximum value for nterms

double e = 1.0; // value of the Euler number

double eps; // max tolerable error

// get values of eps and nmax

cout<<"I am going to compute the value of e \n \n";

cout<<" What is the aceptable error ? ";

cin >> eps;

cout<<" How many terms at most ? ";

cin >> nmax;

// compute the value of e

...

// write the value of e and nterms

...

cin >> nmax;

}

#include <iostream>

void main()

{

int nterms = 0; // number of terms in series

int nmax; // maximum value for nterms

double e = 1.0; // value of the Euler number

double eps; // max tolerable error

// get values of eps and nmax

...

// compute the value of e

{ int fact;

do

{ fact=1;

nterms = nterms + 1;

// compute nterms!

for (int i=1; i<=nterms; i++) fact=fact * i;

e = e + 1.0 / fact;

}

while (2.0 * fact > eps);

}

// write the value of e and nterms

...

cin >> nmax;

}

#include <iostream>

void main()

{

int nterms = 0; // number of terms in series

int nmax; // maximum value for nterms

double e = 1.0; // value of the Euler number

double eps,term; // max tolerable error

// get values of eps and nmax

cout << "I am going to compute the value of e \n \n";

cout << " Give me the acceptable error ? ";

cin >> eps;

cout << " Give me the maximum number number of terms ? ";

cin >> nmax;

// compute the value of e

{

do

{ int fact=1;

nterms = nterms + 1;

// compute nterms!

for (int i = 1; i <= nterms; i++)

fact = fact * i;

term = 1.0 / fact;

e = e + term;

}

while (2.0 * term > eps);

}

// write the value of e and nterms

cout << "\n After " << nterms << " iterations,\n\n"

cout << " the value of e is " << e <<"\n";

cin >>nmax;

}

#include <iostream>

void main()

{

int nterms = 0; // number of terms in series

int nmax; // maximum value for nterms

double e = 1.0; // value of the Euler number

double eps; // max tolerable error

// get values of eps and nmax

...

// compute the value of e

{ int divider = 1;

do

{ nterms = nterms + 1;

divider = divider * nterms;

e = e + 1.0 / divider;

}

while (2.0 / divider > eps);

}

// write the value of e and nterms

...

cin >> nmax;

}