Presentation is loading. Please wait.

Presentation is loading. Please wait.

5.13 Recursion Recursive functions Functions that call themselves

Similar presentations


Presentation on theme: "5.13 Recursion Recursive functions Functions that call themselves"— Presentation transcript:

1 5.13 Recursion Recursive functions Functions that call themselves
Can only solve a base case Divide a problem up into What it can do What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do Eventually base case gets solved Gets plugged in, works its way up and solves whole problem

2 5.13 Recursion Example: factorials Notice that
5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! 4! = 4 * 3! ... Can compute factorials recursively Solve base case (1! = 0! = 1) then plug in 2! = 2 * 1! = 2 * 1 = 2; 3! = 3 * 2! = 3 * 2 = 6;

3 5.13 Recursion

4 fig05_14.c (Part 1 of 2)

5 fig05_14.c (Part 2 of 2) 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 10! =

6 5.14 Example Using Recursion: The Fibonacci Series
Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1) fibonacci( n – 2 ); }

7 5.14 Example Using Recursion: The Fibonacci Series
Set of recursive calls to function fibonacci f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +

8 fig05_15.c (Part 1 of 2)

9 fig05_15.c (Part 2 of 2) Program Output
Enter an integer: 0 Fibonacci( 0 ) = 0 Enter an integer: 1 Fibonacci( 1 ) = 1 Enter an integer: 2 Fibonacci( 2 ) = 1 Enter an integer: 3 Fibonacci( 3 ) = 2 Enter an integer: 4 Fibonacci( 4 ) = 3

10 Program Output (continued)
Enter an integer: 5 Fibonacci( 5 ) = 5 Enter an integer: 6 Fibonacci( 6 ) = 8 Enter an integer: 10 Fibonacci( 10 ) = 55 Enter an integer: 20 Fibonacci( 20 ) = 6765 Enter an integer: 30 Fibonacci( 30 ) = Enter an integer: 35 Fibonacci( 35 ) = Program Output (continued)

11 5.14 Example Using Recursion: The Fibonacci Series

12 5.15 Recursion vs. Iteration
Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case recognized Both can have infinite loops Balance Choice between performance (iteration) and good software engineering (recursion)

13 14.4 Using Command-Line Arguments
Pass arguments to main on DOS or UNIX Define main as int main( int argc, char *argv[] ) int argc Number of arguments passed char *argv[] Array of strings Has names of arguments in order argv[ 0 ] is first argument Example: $ mycopy input output argc: 3 argv[ 0 ]: “mycopy" argv[ 1 ]: "input" argv[ 2 ]: "output"

14 Notice argc and argv[] in main
fig14_03.c (Part 1 of 2) argv[1] is the second argument, and is being read. argv[2] is the third argument, and is being written to. Loop until End Of File. fgetc a character from inFilePtr and fputc it into outFilePtr.

15 fig14_03.c (Part 2 of 2)

16 12.1 Introduction Dynamic data structures Linked lists Stacks Queues
Data structures that grow and shrink during execution Linked lists Allow insertions and removals anywhere Stacks Allow insertions and removals only at top of stack Queues Allow insertions at the back and removals from the front Binary trees High-speed searching and sorting of data and efficient elimination of duplicate data items

17 12.2 Self-Referential Structures
Structure that contains a pointer to a structure of the same type Can be linked together to form useful data structures such as lists, queues, stacks and trees Terminated with a NULL pointer (0) struct node { int data; struct node *nextPtr; } nextPtr Points to an object of type node Referred to as a link Ties one node to another node

18 12.3 Dynamic Memory Allocation
Figure 12.1 Two self-referential structures linked together 10 15

19 12.3 Dynamic Memory Allocation
Obtain and release memory during execution malloc Takes number of bytes to allocate Use sizeof to determine the size of an object Returns pointer of type void * A void * pointer may be assigned to any pointer If no memory available, returns NULL Example newPtr = malloc( sizeof( struct node ) ); free Deallocates memory allocated by malloc Takes a pointer as an argument free ( newPtr );

20 12.4 Linked Lists Linked list
Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Subsequent nodes are accessed via the link-pointer member of the current node Link pointer in the last node is set to NULL to mark the list’s end Use a linked list instead of an array when You have an unpredictable number of data elements Your list needs to be sorted quickly

21 Linked Lists

22 fig12_03.c (Part 1 of 8)

23 fig12_03.c (Part 2 of 8)

24 fig12_03.c (Part 3 of 8)

25 fig12_03.c (Part 4 of 8)

26 fig12_03.c (Part 5 of 8)

27 fig12_03.c (Part 6 of 8)

28 fig12_03.c (Part 7 of 8)

29 fig12_03.c (Part 8 of 8)

30 Program Output (Part 1 of 3)
Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: B The list is: B --> NULL Enter a character: A A --> B --> NULL Enter a character: C A --> B --> C --> NULL ? 2 Enter character to be deleted: D D not found. Enter character to be deleted: B B deleted. A --> C --> NULL Program Output (Part 1 of 3)

31 Program Output (Part 2 of 3)
? 2 Enter character to be deleted: C C deleted. The list is: A --> NULL Enter character to be deleted: A A deleted. List is empty. ? 4 Invalid choice. Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 3 End of run. Program Output (Part 2 of 3)

32 Program Output (Part 3 of 3)
? 4 Invalid choice. Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 3 End of run. Program Output (Part 3 of 3)

33 Linked Lists


Download ppt "5.13 Recursion Recursive functions Functions that call themselves"

Similar presentations


Ads by Google