Loading...

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

The Adobe Flash plugin is needed to view this content

Elementary Programming using Mathcad (INF-20303)

- 11th lecture

Recursion

For example in the series

we use the operator ! factorial.

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.

(No Transcript)

(No Transcript)

Fibonacci

1, 1, 2, 3, 5, 8, 13, 21, ...

Applying recursion

fibonacci(n)

This algorithm expresses clearly that a term

equals the sum of the two preceding terms.

(No Transcript)

Bisection

No tests for roots on the specified interval!

- 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
- Disadvantage inefficient
- calling a function takes time a.o. transport of

arguments - more calls are needed, see fibonacci

Towers of Hanoi

Bring disks from pole A to pole C.

A

B

C

- 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

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

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 .

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

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

Repeat until N lt 10

This works well in Mathcad for N 0, because

Mathcad initializes v 0.

Better is N 0, however infinite loop!

So solves that problem! (in other programming

languages)

(No Transcript)

With error message if given number (N) is

negative

Answer section 19.1, assignment 2 Bubble sort.

Phase 1

Phase 2

Phase 3

Ready

With 5 elements Repeat 5 - 1 4 times. In

general n elements Repeat n-1 times.

- 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

For one pass, we need to repeat

In principle, repeat this last(v) times

(maximum). Vector with n elements last(v) n-1

number of needed repetitions.

- After phase 1 the largest element is in last

position, a.s.o. - so, every phase we have to visit one element

less

Alternative

Sometimes less number of phases, see the example.

We do not need to know the exact number of swaps

Sufficient is

again is a so-called boolean variable!

What is the problem here?

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

k 0

j 1 if v1 lt v0 j 0 if v0 lt v1

Solution

Maximally last(v) phases!

So bubble4 operates ok!

- Sometimes a variable with the values
- 0 (false or not true) or
- 1 (true)
- Assume you want adult to become 1 if age 18
- adult to become 0

if age lt 18 - adult is a so-called boolean variable
- You can accomplish that in two ways

Or