Title: Where are we, and where to go?
1Where are we, and where to go?
Procedural programming, Or structured
programming, Or imperative programming (104),
modularity
OOP (104, 151)
(171)
Data structure Linear list, stack,
queue Nonlinear tree, graph
Simple types of variables (variablesobjects) 3
program structures (assignment, conditional, iter
ation)
Static objects Dynamic objects Functions on
objects
(member) variables
Array, struct pointer
class
objects
operation
Algorithms
(member) functions
C, Java
C, Pascal
Data, variable, object Operation, function,
procedure, subprogram, module, method
AlgorithmsData Structures Programs Niklaus
Wirth
2Programming paradigms
- Imperative programming
- Declarative programming
- Functional (Lisp) and logical (Prolog)
programming - Highly recursive
- Object-oriented programming
- Generic programming
3procedural programming
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()
- int main()
-
- int x,y,z
- int a,b,c
- af1(x)
- bf2(y)
- cf3(z)
-
-
- int f1()
-
-
- int f2()
-
Object oriented programming a sequence of
objects! Objects communicate by sending
messages.
4Communication between functions
- 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!
5Reference X
- int b a
- b is an alternative name for a
void f(int b) int main() int a
f(a)
int a10 int b a int c a b
100 a ???
int b
a
10
b
c
Relationship with pointers (later on)!
6Call by Value
- int f(int x)
-
- cout ltlt value of x ltlt x ltlt endl
- x 4
-
- main()
-
- int v 5
- f(v)
- cout ltlt value of v ltlt v ltlt 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
5
5
7Call by Reference
- int f(int x)
-
- cout ltlt value of x ltlt x ltlt endl
- x 4
-
- main()
-
- int v 5
- f(v)
- cout ltlt value of v ltlt v ltlt 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
8Call by Constant Reference
- int f( const int x )
-
- cout ltlt value of x ltlt x ltlt endl
- x 4 // invalid
-
- main()
- int v 5
- f(v)
- cout ltlt value of v ltlt v ltlt 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.
9Parameter Passing
- 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!
10Return Passing
- 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!
11Scope of variables
- The scope of a declaration is the block of code
where - the 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
12Global (local to the file)
Local to blocks
Local to functions
int x int main() x0 cout ltlt x ltlt
endl int x x1 int x x2
cout ltlt x ltlt endl cout ltlt x ltlt endl
void f() int x x1 int x x2
cout ltlt x ltlt endl cout ltlt x ltlt endl
- int main()
-
- int x,y,z
-
-
- void f()
-
- int x
-
13In a for-loop
- int i
- for (i1ilt10i) cout ltlt Ai
-
- for (int i1ilt10i) cout ltlt Ai
equivalent
14Global 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!
15Summary
Pass by value
Global variable
Pass by reference
int MIN void min(int,int) int main() int
x,y cin gtgt x gtgt y gtgt endl min(x,y)
cout ltlt MIN void min(int a, int b) if
(altb) MINa else MINb
int min(int,int) int main() int
x,y,mini cin gtgt x gtgt y gtgt endl
minimin(x,y) cout ltlt mini int min(int
a, int b) int m if (altb) ma
else mb return (m)
void min(int,int,int) int main() int
x,y,mini cin gtgt x gtgt y gtgt endl
min(x,y,mini) cout ltlt mini void min(int
a, int b, int m) if (altb) ma else
mb
Good style!!!
16Declarative 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
17Important for algorithm design and analysis
18The 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!
19More declarative than procedural! what vs. how
- // move n disks from A to C via B
- void tower(int n, char A, char B, char C)
- if (n1) 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 ltlt move disc ltlt k ltlt
- from ltlt X ltlt
- to Y ltlt endl
-
20Trace tower(4,A,B,C)
21Declarative with recursion Seems to be more
automatic!
Normal (non-recursive) functions
Recursive function
void three() void two ()
three() void one () two() void
main() one()
int fac(int n) int product if(n lt 1) product
1 else product n fac(n-1) return
product void main() fac(3)
- Functions are calling (DIFFERENT) functions
- One function (three) is the last stopping
function
- calling the SAME function ( with different
parameters) - The stopping function is already included as a
condition
22Recursive 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!
23Recursion 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!
24Everything 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.
25For n elements
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)
26Exponential
- How to write exp(int x, int y) recursively?
- int exp(int x, int y)
- int power
- if(y0) power 1
- else power x exp(x, y-1)
- return power
27Sum 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(size0) sum0
- else sumasum(a,size-1)asize-1
- return sum
28Product 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(size0) prod1
- else prodaprod(a,size-1)asize-1
- return prod
29Counting 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 (nlt10) if (n0) z1
- else z0
- else zzeros(n/10)zeros(n10)
-
- return z
- n/10 ? the number n with the last digit removed
- n10 ? the last digit of n
30Find factors
- Write a recursive function to determine how many
factors m are part of n. For example, if n48 and
m4, then the result is 2 (48443). - int factors(int n, int m)
- int f
- if(nm ! 0) f0
- else f1factors(n/m, m)
- return f
31Binary search
- int bsearch(int data,lower,upper,value)
- if (lowerltupper)
- mid(lowerupper)/ 2
- if (datamid value) posmid
- else if (datamidgtvalue)
- posbsearch(data,lower,mid1,value
) - else
- posbsearch(data,mid1,upper,value
) -
- return pos
-
-
32Sorting
- Take the minimum, then sort the remaining
elements