32
Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects Dynamic objects Functions on objects Procedural programming, Or structured programming, Or imperative programming (104), modularity (member) variables OOP (104, 151) Data structure: Linear: list, stack, que Nonlinear: tree, graph Algorithms Algorithms+Data Structures = Programs Niklaus Wirth (171) (member) functions Array, struct pointer objects a, variable, object ration, function, procedure, subprogram, module, method operation class C, Pascal C++, Java

Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 1

Where are we, and where to go?

Simple types of variables

(variables=objects)

3 program structures

(assignment,

conditional,

iteration)

Static objects

Dynamic objects

Functions on objects

Procedural programming,

Or structured programming,

Or imperative programming (104), modularity

(member) variables

OOP (104, 151)

Data structure:

Linear: list, stack, queue

Nonlinear: tree, graph

Algorithms

Algorithms+Data Structures = Programs

Niklaus Wirth

(171)

(member) functions

Array, struct

pointer objects

Data, variable, object

Operation, function, procedure, subprogram, module, method

operation

class

C, Pascal C++, Java

Page 2: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 2

Programming paradigms

Imperative programming Declarative programming

Functional (Lisp) and logical (Prolog) programming Highly recursive

Object-oriented programming

Generic programming

Page 3: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 3

int main(){ int x,y,z; int a,b,c;

a=f1(x);b=f2(y);c=f3(z);…

}

int f1(){}

int f2(){}

int f3(){}

main(), is the first function, and is composed of a sequence of ‘procedures’

(or ‘functions’ in C++).

Functions communicate by passing parameters.int main(){ A a; B b; C c;

a.f1(); b.f2(); c.f3(); …}

Class A{Int x;Int f1();}

Class B{Int y;Int f2()}

Class C{Int z;Int f3();}

procedural programming:

Object oriented programming: a sequence of ‘objects’!

Objects communicate by sending messages.

Page 4: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 4

Pass by value: formal parameters and arguments are different variables.

ideal desirable behavior

(but not efficient some times)

Pass by reference: they are the same variables, but different names!

should carefully handled!

Communication between functions:

Page 5: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 5

Reference: X& int& b a; b is an alternative name for a

void f(int& b) {};

int main() {

int a;

f(a);

}

int a=10;

int& b = a;

int& c = a;

b = 100;

a ???

int& b;

10a

b

cRelationship with pointers (later on)!

Page 6: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 6

int f(int x) { cout << “value of x = “ << x << endl;x = 4; }

main() { int v = 5;f(v);cout << “value of v = “ << v << endl;}

Output: Value of x = Value of v = When a variable v is passed by value to a function f, its value is copied to

the corresponding variable x in f Any changes to the value of x does NOT affect the value of v Call by value is the default mechanism for parameter passing in C++

5

5

Call by Value

Page 7: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 7

int f(int &x) { cout << “value of x = “ << x << endl;x = 4;}

main() { int v = 5;f(v);cout << “value of v = “ << v << endl;}

Output: Value of x = Value of v = When a variable v is passed by reference to a parameter x of

function f, v and the corresponding parameter x refer to the same variable

Any changes to the value of x DOES affect the value of v

5

4

Call by Reference

Page 8: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 8

int f( const int &x )

{

cout << “value of x = “ << x << endl;

x = 4; // invalid

}

main() {

int v = 5;

f(v);

cout << “value of v = “ << v << endl;

}

Passing variable v by constant reference to parameter x of f will NOT allow any change to the value of x.

It is appropriate for passing large objects that should not be changed by the called function.

Call by Constant Reference

Page 9: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 9

Call by value for small objects that should not be changed by the function

Call by constant reference for large objects that should not be changed by the function

Call by reference is appropriate for all objects that may be changed by the

function, not recommended!!! rare!

Parameter Passing

Page 10: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 10

return by value, for small objects that should not be changed by the function

return by constant reference, for large objects that should not be changed by the function

return by reference, for all objects that may be changed by the function, not recommended!!! rare!

Return Passing

Page 11: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 11

Scope of variablesThe scope of a declaration is the block of code wherethe identifier is valid for use.

A global declaration is made outside the bodies of all functions and outside the main program. It is normally grouped with the other global declarations and placed at the beginning of the program file.

A local declaration is one that is made inside the body of a function.

Locally declared variables cannot be accessed outside of the

function they were declared in. Local to a function

(the variables in Main are also local, local to ‘main’ function) It is possible to declare the same identifier name in different parts of

the program: local to a block

Some code enclosed in braces

Page 12: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 12

int main(){ int x,y,z; …}

void f(){

int x; …}

void f(){

int x; x=1; { int x; x=2; cout << x << endl;

} cout << x << endl;}

int x;int main(){

x=0;cout << x << endl;

int x; x=1; { int x; x=2; cout << x << endl;

} cout << x << endl;}

Local to blocksLocal to functions Global (local to the file)

Page 13: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 13

In a for-loop

{ int i;

for (i=1;i<10;i++) cout << A[i];

}

for (int i=1;i<10;i++) cout << A[i];

equivalent

Page 14: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 14

Global Variables

Undisciplined use of global variables may lead to confusion and debugging difficulties.

Instead of using global variables in functions, try passing local variables by reference.

It is forbidden in structured programming!

Page 15: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 15

int MIN;

void min(int,int);

int main() {

int x,y;

cin >> x >> y >> endl;

min(x,y);

cout << MIN;

}

void min(int a, int b)

{

if (a<b) MIN=a;

else MIN=b;

}

void min(int,int,int&);

int main() {

int x,y,mini;

cin >> x >> y >> endl;

min(x,y,mini);

cout << mini;

}

void min(int a, int b, int& m)

{

if (a<b) m=a;

else m=b;

}

int min(int,int);

int main() {

int x,y,mini;

cin >> x >> y >> endl;

mini=min(x,y);

cout << mini;

}

int min(int a, int b)

{

int m;

if (a<b) m=a;

else m=b;

return (m);

}

Summary

Global variable Pass by reference Pass by value

Good style!!!

Page 16: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 16

Declarative vs. Procedural

What to do vs. how to do

Interface vs. actions

Separate compilation

*.h (declaration) vs *.cc (actions, procedures)

Functional programming

A procedure is more a ‘mathematical function’

Page 17: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Recursion

Important for algorithm design and analysis

Page 18: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 18

The tower of Hanoi

Move a stack of disks of different sizes from one rod to another

through a third one:

- only one disk is moved each time

- always smaller ones on top of bigger ones

Check any webpage!

Page 19: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 19

// move n disks from A to C via B

void tower(int n, char A, char B, char C) {if (n==1) move(1,A,C);else {tower(n-1,A,C,B);

move(n,A,C); tower(n-1,B,A,C)};

}

void move(int k, char X, char Y) {cout << “move disc ” << k <<

“ from “ << X << “ to “ Y “ << endl;}

More declarative than procedural!

what vs. how

Page 20: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 20

Trace tower(4,A,B,C)

Page 21: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 21

void three(…) {…

}void two (…) { three();}void one (…) { two(…);}

void main() { one(…);}

Functions are calling (DIFFERENT) functions

One function (three) is the last ‘stopping function’

int fac(int n){

int product;

if(n <= 1) product = 1;

else product = n * fac(n-1);return product;

}

void main(){ fac(3);}

… calling the SAME function ( with different parameters) …

The ‘stopping function’ is already included as a ‘condition’

Normal (non-recursive) functions

Recursive function

Declarative with recursion

Seems to be more ‘automatic’!

Page 22: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 22

Recursive function

A recursive function is just a function which is calling one (or more) other functions which happen to be the same!!!

Though the function is the same, ‘parameters’ are always ‘smaller’

There is always at least one stopping case to terminate

It is a kind of ‘loop’, even more powerful as a general problem-solving technique! --- thinking recursively!

Page 23: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 23

Recursion vs. Iteration (non-recursive)

A recursive solution may be simpler to write (once you get used to the idea) than a non-recursive solution.

But a recursive solution may not be as efficient as a non-recursive solution of the same problem.

To iterate is human, to recurse, divine!

Page 24: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 24

Everything is recursive …

Linear search

Length of a string

Min, max of an array

Selection sort

Bubble sort …

Binary search: Compare search element with middle element of the array: If not equal, then apply binary search to half of the array (if not

empty) where the search element would be.

Page 25: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 25

Start from the first element

While (not yet finished) do

do the current element

move to the next one

toto(n)

If 0 or 1 element, just do it

else decompose into first element and the n-1 remaining elements

do the first element

toto(n-1)

For n elements:

Page 26: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 26

Exponential

How to write exp(int x, int y) recursively?

int exp(int x, int y) { int power; if(y==0) power = 1; else power = x * exp(x, y-1); return power;

}

Page 27: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 27

Sum of the array

Write a recursive function that takes a double array and its size as input and returns the sum of the array:

double asum(int a[], int size){ double sum; if(size==0) sum=0;

else sum=asum(a,size-1)+a[size-1]; return sum;}

Page 28: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 28

Product of an array

Write a recursive function that takes a double array and its size as input and returns the product of the array:

double aprod(int a[], int size) { doulbe prod; if(size==0) prod=1; else prod=aprod(a,size-1)*a[size-1]; return prod;

}

Page 29: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 29

Counting the number of zeros Write a recursive function that counts the number of zero digits in a

non-negative integer zeros(10200) returns 3

int zeros(int n){ int z;

if (n<10) if (n==0) z=1; else z=0;

else z=zeros(n/10)+zeros(n%10);

return z;}

n/10 the number n with the last digit removed n%10 the last digit of n

Page 30: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 30

Find factors

Write a recursive function to determine how many factors m are part of n. For example, if n=48 and m=4, then the result is 2 (48=4*4*3).

int factors(int n, int m){int f;if(n%m != 0) f=0;else f=1+factors(n/m, m);return f;

}

Page 31: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 31

int bsearch(int data[],lower,upper,value) {if (lower<=upper) { mid=(lower+upper)/ 2;

if (data[mid] == value) pos=mid;}; else if (data[mid]>value)

pos=bsearch(data,lower,mid–1,value); else pos=bsearch(data,mid+1,upper,value); }

return pos;}

Binary search

Page 32: Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects

Slide 32

Take the minimum, then sort the remaining elements …

Sorting