Chapter 2 01
This presentation is the property of its rightful owner.
Sponsored Links
1 / 56

Chapter 2.01 PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

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 :

Download Presentation

Chapter 2.01

An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 2 01

Chapter 2.01

C++

An Introduction


Fourth generation systems 1970 now

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.

Fourth Generation Systems1970-now


C and c

C and C++

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++


A first example

A first example

#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);

}


Terminal symbols

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

Terminal Symbols


Chapter 2 01

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

Comments

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.


The main function

voidmain()

{

...

}

The main function

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”


Data declarations

PROGRAM

=

Description of data

+

Actions to perform upon these data

Data Declarations

#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 and variables

Constants

(Name)

Value

The value of a

constant belongs

to a type.

Variables

Name

Type

Type =

set of all values

the variable can have

Constants and Variables

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


Constants data whose value can not change during program execution

Literal Constants

Only a value:

Named Constants

A name and a value:

The value can be a constant expression:

ConstantsData whose value can not change during program execution

3.1416

"The area of this circle is "

double pi = 3.1416;

double twopi = pi * 2.0;


Why named constants

Why named constants ?

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;


Types in c and c

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

Types in C and C++


The type int

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==,!=,>=,<=,>,<

The type int


The type double

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==,!=,>=,<=,>,<

The type double


Aritmetic expressions

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

Aritmetic Expressions

(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


Concatenated operators

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:

Concatenated Operators

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++ .


The type enum

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

The type enum


The type enum1

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;

The type enum


The type boolean

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

The type boolean

X = TRUE

X = FALSE

! X

FALSE

TRUE


Relational expressions

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

Relational Expressions

have a boolean value

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


Block structures

Block Structures

#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);

}


Block structures1

Block Structures

A program is a set of nested blocks

{ ...

{ ...

{ ...

} // RED

{ ... } // GREEN

} // PURPLE

} // BLUE


Block structures2

Block Structures

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 =...; ... }


Block structures3

Block Structures

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 =...; ... }


Executable instructions

Executable Instructions

#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


Output commands

Output Commands

#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”


Input commands

Input Commands

#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;


Arithmetic assignment

Arithmetic assignment

#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


Control statements

Control Statements

#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


Control statements in c

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

...

Control Statements in C++


If b s1 else s2

B

B

FALSE

TRUE

FALSE

TRUE

S2

S1

S2

S1

if (B) S1 else S2;

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

elsecout << “impair”;


If b s

B

B

FALSE

TRUE

FALSE

TRUE

S

S

if (B) S;

// absolute value of x

if(x < 0) x = -x;


Switch e case a s1 case b s2 case c s3 default s4

e == a

Yes

S1

No

e == b

Yes

No

S2

e == c

Yes

No

S3

S4

switch (e)

switch(e){case a :S1;case b :S2; case c :S3;default:S4; }

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

int,enum,char.


Chapter 2 01

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


Chapter 2 01

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


Chapter 2 01

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


While b s

while (B) S;

B

B

FALSE

S

TRUE

S

while(status != endoftape)

{PlaySong}


Computing the gcd 1

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

Computing the GCD (1)


Computing the gcd 2

Top level design :

Read value of x and y;

Compute g, the gcd of x and y;

Write the value of g;

Computing the GCD (2)

#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

}


Computing the gcd 4

Computing the GCD (4)

#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;

}


Computing the gcd 5

Computing the GCD (5)

#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;

}


Computing the gcd 6

Computing the GCD (6)

#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;

}


Computing the gcd 7

Computing the GCD (7)

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


Do s while b

S

S

B

TRUE

B

FALSE

do S while (B)

do

{RemoveBolt}

while (BoltsLeft != 0)


The for loop

init

cond

FALSE

TRUE

S

change

The for loop

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}


Computing factorial n 1

Top level design :

Read value of n;

Compute fact = n!;

Write the value of fact;

Computing factorial n (1)

#include<iostream>

void main()

{ intfact,n;

// Read value of n

...

// Compute fact = n!

...

// Write value of n and fact

...

cin >> n;

}


Computing factorial n 2

Computing factorial n (2)

#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

...

}


Computing factorial n 3

Computing factorial n (3)

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


Computing e by a convergent series

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 + + + + + ) <

Computing eby a convergent series

Mathematics :


Computing e by a convergent series1

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

Computing eby a convergent series


Computing e by a convergent series2

#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 !

}

Computing eby a convergent series


Computing e by a convergent series3

#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;

}

Computing eby a convergent series


Computing e by a convergent series4

#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;

}

Computing eby a convergent series


Computing e by a convergent series5

#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;

}

Computing eby a convergent series


Computing e by a convergent series6

#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;

}

Computing eby a convergent series


  • Login