Title: Presentation Number 6
1Presentation Number 6
2Last Time in Class
- float mat33
- mat02 5.0f
- mat00 mat10 4.0f
Multi-dimensional arrays
mat
3Functions
- Function definition
- return-type name(arg_type1 arg_name1, arg_type2
arg_name2, ) -
- function body
- return value
-
- Return statement.
- Void type.
- Call by-value.
4Functions
- Function declaration
- return-type name(arg_type1 arg_name1, arg_type2
arg_name2, )
5Questions?
6Assertions
- include ltassert.hgt
- include ltstdio.hgt
- int f( int a, int b )
- int g( int c )
- int main( void )
-
- int a 0, b 0, c 0
- ....
- scanf( dd, a, b )
- .....
- c f( a,b )
- assert( c gt 0 )
- .....
An assertion makes sure that the statement
evaluates to true.
If the assertion evaluates to false it aborts the
run of the program and prints a message to the
screen.
an assertion
7Assertions
- Assertions are useful when debugging an
application. - They require the inclusion of ltassert.hgt
- Assertions work only in debug mode.
- They will not have no effect in an optimized
version of the application. - You can use them when debugging your homework ...
However do not leave them in the submitted code.
8Thinking About Algorithms
9Advanced Calculator Explained
- Input
- Output 8
- Assuming that we solved the problem for part of
the array and we are now working on the ith
element ... which part of the array do we want to
have solved? - Do we want to have the length of the sequence for
elements i1 to n-1 or elements 0 to i-1?
10Elements i1 to n-1?
- Elements
- Lengths
- Can I use information about the next elements to
calculate the length of the sequence for the
current element? - Lengths stores the length of the sequence that
begins at element j. - If (element j gt element i) then length i 1
length j
11Solution
- Define an additional array that will store the
sequence lengths for each element (initialized to
1). - Solve the problem for element i (with i running
from n-1 to 0). - Compare element i to element j (with j running
from i1 to n-1). - Length i equals maxlength i, 1 length j.
12Elements 0 to i-1?
- Elements
- Lengths
- Can I use information about the previous elements
to calculate the length of the sequence for the
current element? - Lengths stores the length of the sequence that
ends at element j. - If (element j lt element i) then length i 1
length j
13Solution
- Define an additional array that will store for
each element (initialized to 1) the length of the
sequence that ends at that element. - Solve the problem for element i (with i running
from 0 to n-1). - Compare element i to element j (with j running
from 0 to i-1). - Length i equals maxlength i, 1 length j.
14Recursions
15Recursion
- Definition 1
- Recursion - a method for defining functions in
which the function being defined is used within
its own definition. - Definition 2
- Recursion - see recursion.
- Definition 3
- Recursion If you still dont get it See
Recursion.
16Recursion
- n! 1 2 3 ... (n-1) n
- This is equivalent to
- n! (n-1)! n
- Thus, we can also define factorial to be
-
- 0! 1 n 0
- n! (n-1)! n n gt 0
This is a recursive definition
17A recursive definition
- C functions can call themselves!
- When a function calls itself we call this a
recursive calls. - Two things you must remember about recursions
- Change the parameters from call to call!!!
- Make sure you defined a boundary condition!!!
18Factorial Example
reucrsive version
- int factorial ( int n )
-
- if ( n lt 1 )
- return 1
- else
- return ( n factorial( n 1 ) )
-
- int factorial( int n )
-
- int product 1
- for( n gt 1 --n )
- product n
- return product
boundary condition
recursive call
iterative version
RecFactorial.c
19Another Way To Think About It
- int factorial ( int n )
-
- if ( n lt 1 )
- return 1
- else
- return ( n factorial( n 1 ) )
-
- In other words ... I know how to solve a simple
problems. - Let the function deal with the complicated
problems.
I know how to solve this factorial
I know that n! n (n-1)! Let the function
solve the (n-1)!
20Examples
21Write Backwards
- include ltstdio.hgt
- void wrt_back()
- int main(void)
-
- printf( Input a line )
- wrt_back()
- printf( \n\n )
- return 0
-
- void wrt_back()
-
- int c 0
- if ( ( c getchar() ) ! \n )
- wrt_back()
- putchar( c )
Call order
Print order
RecReverse.c
22Recursive Power
- double power( double val, unsigned int pow )
-
- if ( pow 0 )
- return( 1.0 )
- else
- return( power( val, pow 1 ) val )
-
pow( X, 0 ) returns 1
23Fibonacci Number
- int fib( int num )
-
- switch( num )
-
- case 0
- return(0)
- break
- case 1
- return(1)
- break
- default
- return( fib( num 1 ) fib( num 2 ) )
- break
-
-
the recursive calls
Fibonacci.c
24Trace Fibonacci
In this implementation the function calls itself
twice
Recursion can consume a lot of memory (possible
stack overflow) and are therefore considered
inefficient.
25Iterative Solution
- int fib( int num )
-
- int i 0, fn 0, fn_1 1, fn_2 2
- if ( num 0 )
- return 0
- if (num 1)
- return 1
- for ( i 2 i lt num 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
26Hanoi Towers
27Mutually Recursive Functions(Hanoi Towers)
- include ltstdio.hgt
- void move_right(int id)
- void move_left(int id)
- void move_right(int id)
-
- if ( 0 id ) return
- move_left( id 1 )
- printf( Shift disc d one peg to right.\n, id
) - move_left(id - 1)
-
move id 1 discs one peg to the left
move id 1 discs one peg to the left
Hanoi.c
28Mutually Recursive Functions(Hanoi Towers)
- void move_left( int id )
-
- if ( 0 id ) return
- move_right( id 1 )
- printf( Shift disc d one peg to left.\n, id
) - move_right( id 1 )
-
- int main( void )
-
- move_left( 3 )
- return 0
-
move id 1 discs one peg to the right
move id 1 discs one peg to the right
solve the 3 disc problem
Hanoi.c