- By
**slone** - Follow User

- 84 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Chapter 2.01' - slone

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

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

#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 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 SymbolsSource 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 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()

{

...

}

The main functionEvery 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”

=

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

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

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 execution3.1416

"The area of this circle is "

double pi = 3.1416;

double twopi = pi * 2.0;

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;

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

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 Operatorsx = x+1;can be written as x++; 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 right side of an assignment.

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 enumMain benefit : clarity right side of an assignment.

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 enumPossible values: right side of an assignment.

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 booleanX = TRUE

X = FALSE

! X

FALSE

TRUE

a == b right side of an assignment. TRUE when a equals b

a != b TRUE when a different from b

a > b TRUE when a greater than b

a < b TRUE when a less than b

a >= b TRUE when a greater or equal b

a <= b TRUE when a less or equal b

Relational Expressionshave a boolean value

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

Block Structures right side of an 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);

}

Block Structures right side of an assignment.

A program is a set of nested blocks

{ ...

{ ...

{ ...

} // RED

{ ... } // GREEN

} // PURPLE

} // BLUE

Block Structures right side of an assignment.

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 Structures right side of an assignment.

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 right side of an 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);

}

PROGRAM

=

Description of data

+

Actions to perform upon these data

Output Commands right side of an 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);

}

Cout << constant string;

Cout << integer, character or double variable;

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

Input Commands right side of an 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);

}

cin >> integer, character or double variable;

Arithmetic assignment right side of an 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 right side of an 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);

}

{…}

Radius != 0

FALSE

Selection statements right side of an assignment.

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++B right side of an assignment.

B

FALSE

TRUE

FALSE

TRUE

S2

S1

S2

S1

if (B) S1 else S2;if(x%2 == 0) cout << “pair”;

elsecout << “impair”;

B right side of an assignment.

B

FALSE

TRUE

FALSE

TRUE

S

S

if (B) S;// absolute value of x

if(x < 0) x = -x;

e == a right side of an assignment.

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.

switch (e) right side of an assignment.

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 right side of an assignment.

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) right side of an assignment.

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

Specifications : right side of an assignment.

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)Top level design : right side of an assignment.

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 right side of an assignment.(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 right side of an assignment.(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 right side of an assignment.(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 right side of an assignment.(7)

Program states for 123 and 456 as inputs

Executed instr. x y g

Cin >>x >>y 123 456 ?

y = y – x 123 333 ?

y = y – x 123 210 ?

y = y – x 123 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 right side of an assignment.

S

B

TRUE

B

FALSE

do S while (B)do

{RemoveBolt}

while (BoltsLeft != 0)

init right side of an assignment.

cond

FALSE

TRUE

S

change

The for loopfor (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 : right side of an assignment.

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 right side of an assignment.(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 right side of an assignment.(3)

Program states for n = 5

Executed instr. n fact i

cin >>n 5 1

i = 1 5 1 1

fact = fact * i 5 1 1

i++ 5 1 2

fact = fact * i 5 2 2

i++ 5 2 3

fact = fact * i 5 6 3

i++ 5 6 4

fact = fact * i 5 24 4

i++ 5 24 5

fact = fact * i 5 120 5

The value of e is given by the infinite series: right side of an assignment.

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

Top level design: right side of an assignment.

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#include right side of an assignment. <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#include right side of an assignment. <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#include <iostream> right side of an assignment.

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#include <iostream> right side of an assignment.

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#include <iostream> right side of an assignment.

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

Connecting to Server..