# 11th lecture - PowerPoint PPT Presentation

PPT – 11th lecture PowerPoint presentation | free to view - id: b3a86-ZjVmM

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## 11th lecture

Description:

### 11th lecture. Elementary Programming. using Mathcad (INF-20303) 2. Recursion ... fibo(5) fibo(4) fibo(3) fibo(2) fibo(1) fibo(3) fibo(2) fibo(2) fibo(1) 9 ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 37
Provided by: Infor374
Category:
Tags:
Transcript and Presenter's Notes

Title: 11th lecture

1
• 11th lecture

2
Recursion
For example in the series
we use the operator ! factorial.
3
In the algorithm for the series we have used
Along this line
So factorial(5) calls factorial(4),
factorial(4) calls factorial(3),
factorial(3) calls factorial(2), etc.
4
(No Transcript)
5
(No Transcript)
6
Fibonacci
1, 1, 2, 3, 5, 8, 13, 21, ...
7
Applying recursion
fibonacci(n)
This algorithm expresses clearly that a term
equals the sum of the two preceding terms.
8
(No Transcript)
9
Bisection
No tests for roots on the specified interval!
10
• Layout of a recursive function
• no recursive calls needed (stopping case), see
factorial(1)
• one or more cases where a recursive call is
needed

Make sure the function stops!
• Advantage formulation is simple and clear
• calling a function takes time a.o. transport of
arguments
• more calls are needed, see fibonacci

11
Towers of Hanoi
Bring disks from pole A to pole C.
A
B
C
12
• n1 A to C
• n 2 A to B. A to C. B to C
• n 3 Use step n 2 to move the first two disks
to B while
• using C as temporary storage.
• A to B. Use step n 2 to move these
two disks
• from B to C using A as temporary
storage
• n-2 ..
• n-1 Use step n-2 to move n-2 disks from A to B
(C temporary).
• Move A to C.
• Use step n-2 to move the n-2 disks from
B to C (A temporary)
• n Use step n-1 to move n-1 disks from A to B
(C temporary)
• Move A to C.
• Use step n-1 to move the n-1 disks from
B to C (A temporary)

Demo
13
• n1 Move A to C
• Use step n-1 to move n-1 disks from A to B (C
temporary)
• Move A to C
• Use step n-1 to move the n-1 disks from
B to C (A temporary)

14
Answers section 19.1, assignment 1 Make a vector
with the digits of a given integer value.
• For example 1325, how we can find the digits?
• divide 1325 by 1000 gives 1
• then 1325 - 1000 325
• divide 325 by 100 gives 3
• then 325 - 300 25
• divide 25 by 10 gives 2
• then 25 - 20 5
• divide 5 by 1 gives 5

Needed number of digits of the value?
We can find the number, however .
15
• A better approach
• determine remainder on division by 10, gives 5
• calculate result of division 132510 132.5
• truncate 132.5 with function floor, gives 132
• determine remainder on division by 10, gives 2
• calculate result of division 13210 13.2
• truncate 13.2 with function floor, gives 13
• determine remainder on division by 10, gives
3
• calculate result of division 1310 1.3
• truncate 1.3 with function floor, gives 1

5
1
2
3
Digits appear in reverse order.
16
• Core of the algorithm is
• determine remainder on division by 10
• calculate result of division by 10
• truncate result of division.

x is digit, insert in vector
Before the loop!
17
Repeat until N lt 10
18
This works well in Mathcad for N 0, because
Better is N 0, however infinite loop!
19
So solves that problem! (in other programming
languages)
20
(No Transcript)
21
With error message if given number (N) is
negative
22
Answer section 19.1, assignment 2 Bubble sort.
Phase 1
Phase 2
Phase 3
23
With 5 elements Repeat 5 - 1 4 times. In
general n elements Repeat n-1 times.
24
• Suppose the algorithm is arrived at element j
• of vector v
• The algorithm looks backwards, a.o. this means
• algorithm starts at element
• test whether the element should be exchanged

in v n elements
25
For one pass, we need to repeat
26
In principle, repeat this last(v) times
(maximum). Vector with n elements last(v) n-1
number of needed repetitions.
27
• After phase 1 the largest element is in last
position, a.s.o.
• so, every phase we have to visit one element
less

28
Alternative
29
Sometimes less number of phases, see the example.
30
We do not need to know the exact number of swaps
Sufficient is
again is a so-called boolean variable!
31
What is the problem here?
32
k 4
k 1, again 1
20 10 7 50 3
50 20 10 7 3
20 50 10 7 3
20 10 50 7 3
7 3 10 20 50
3 7 10 20 50
20 10 7 3 50
k 3, again 1
k 0, again 1
20 10 7 3 50
10 20 7 3 50
10 7 20 3 50
10 7 3 20 50
Conclusion one more phase. However, we are
ready but the algorithm does not know!
k 2, again 1
10 7 3 20 50
7 10 3 20 50
7 3 10 20 50
33
k 0
j 1 if v1 lt v0 j 0 if v0 lt v1
34
Solution
Maximally last(v) phases!
35
So bubble4 operates ok!
36
• Sometimes a variable with the values
• 0 (false or not true) or
• 1 (true)
• Assume you want adult to become 1 if age 18