Title: Presentation Number 8
1Presentation Number 8
2Functions
- Function definition
- return-type name(arg_type1 arg_name1, arg_type2
arg_name2, ) -
- function body
- return value
-
3Recursion
- Definition 1
- A method for defining functions in which the
function being defined is used within its own
definition. - You must define a stopping criteria or you get an
infinite loop. - Definition 2
- Recursion If you still dont get it See
Recursion.
4Recursion
n! n (n-1) (n-2) ... 3 2 1
an iterative definition
n! n (n-1)! 1! 1
a recursive definition
6! 6 5!
4! 4 3!
5! 5 4!
3! 3 2!
2! 2 1!
1! 1
5A recursive definition
- C functions can call themselves!
- When a function calls itself we call this a
recursive call. - Two things you must remember about recursions
- Change the parameters from call to call!!!
- Make sure you defined a boundary condition!!!
6Factorial Example
Reucrsive Version
Iterative Version
int factorial(int n) if (n lt 1) return
1 else return (n factorial(n1))
int factorial(int n) int product 1 int i
1 for( i1 i lt n i) product
i return product
Boundary condition
Recursive call
RecFactorial.c
7Another Way To Think About It
- I know how to solve simple problems.
- Let the function deal with the complicated ones.
int factorial(int n) if (n lt 1) return
1 else return (n factorial(n1))
I know how to solve this
I know that n! n (n-1)! Let someone else
solve (n-1)!
8Hanoi Towers
9Towers of Hanoi
- Goal
- Move the entire tower to the right peg.
- Constraints
- Only move one disk at a time.
- Never place a larger disk on a smaller one.
10Towers of Hanoi
Step 1 Move n 1 disks to the middle peg.
11Towers of Hanoi
Step 2 Move a single disk from the left peg to
the right peg.
12Towers of Hanoi
Step 3 Move n 1 disks from the middle peg to
the right peg.
13Towers of Hanoi
- void hanoi(int size, int src, int dest, int aux)
-
- if ( size 1 )
-
- printf( "Shift disc from peg d to peg
d.\n", src, dest ) - return
-
- hanoi( size 1, src, aux, dest )
- hanoi( 1, src, dest, aux )
- hanoi( size 1, aux, dest, src )
-
- int main()
-
- hanoi( 3, 1, 3, 2 )
- return 0
-
Hanoi.c
14Towers of Hanoi
Call order for Hanoi of size 4
H(4)
H(3)
H(3)
H(2)
H(2)
H(2)
H(2)
H(1)
H(1)
H(1)
H(1)
H(1)
H(1)
H(1)
H(1)
15Write Backwards
- include ltstdio.hgt
- void wrt_back()
-
- int c 0
- if ((c getchar()) ! \n)
- wrt_back()
- printf(c, c)
-
- int main()
-
- printf(Input a line )
- wrt_back()
- printf(\n)
- return 0
16Fibonacci Number
- int fib(int num)
-
- switch(num)
-
- case 1
- return 1
- break
- case 2
- return 1
- break
- default
- return(fib(num1) fib(num2))
- break
-
- return 0
-
the recursive calls
Fibonacci.c
17Trace Fibonacci
In this implementation the function calls itself
twice
Recursion can consume a lot of memory (possible
stack overflow) and are therefore considered
inefficient.
18Iterative Solution
- int fib(int n)
-
- int i 0, fn 0, fn_1 1, fn_2 1
- if (n 1)
- return 1
- if (n 2)
- return 1
- for (i3 iltn i)
-
- fn fn_2 fn_1
- fn_2 fn_1
- fn_1 fn
-
- return fn
-
Recursive functions are sometimes the simplest
and shortest answers
There is always an alternative non-recursive
solution available
Fibonacci.c
19Eight Queens
20Eight Queens
How do you place 8 queens on a chess board
without having them threaten each other?
21- include ltstdio.hgt
- include ltmath.hgt
- include ltassert.hgt
- void print (int arr, int size)
- int checkPosition (int arr, int col, int row)
- int permutations (int arr, int col, int size)
- int main()
-
- int arr 8, num
- num permutations (array, 0, 8)
- printf ("Number of possible solutions d\n",
num) - return 0
Array stores for each row the column number where
a queen is placed
Queens.c
22Generate all the legal possibilities recursively
- int permutations (int arr, int col, int size)
-
- int row 0, num 0
- if (col size) / only one permutation in this
case / -
- print (arr, size)
- num 1
-
- else if ( col lt size )
-
- for (row 0 row lt size row)
-
- if ( checkPosition (arr, col, row) )
-
- arrcol row
- num permutations (arr, col 1, size)
-
-
-
The first pos positions are fixed. Place the next
position and recursively call the function with
the next positions.
Queens.c
23Check if a partial permutation is legal. The
first pos positions do not occupy column value
diagonals are not occupied
- int checkPosition (int arr, int col, int row)
-
- int i
-
- for (i 0 i lt col i)
-
- if ( arri row (col - i) abs(arri
- row) ) - return 0
-
- return 1
Queens.c
24Print a permutation
- void print(int arr, int size)
-
- int i, j
- for (i 0 i lt size i)
-
- for (j 0 j lt size j)
- printf ("c ", (arri j)? '' '-')
- printf ("\n")
-
- printf ("\n\n")
Queens.c
25Merge Sort
- A recursive method for sorting an array.
- Question
- How do you merge two sorted arrays into one
sorted array?
26Merge-Sort
Step 1 Sort the left half of the array.
27Merge-Sort
Step 2 Sort the right half of the array.
28Merge-Sort
Step 3 Merge the two halves into one.