5.13 Recursion Recursive functions Functions that call themselves

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Introduction to Data Structures Systems Programming Concepts.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Programming Practice 3 - Dynamic Data Structure
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 19: Recursion.
Functions Course conducted by: Md.Raihan ul Masood
Pointers and Linked Lists
Chapter 12 – Data Structures
Recursion what is it? how to build recursive algorithms
Chapter 15 Recursion.
Dynamic Allocation Review Structure and list processing
C Functions -Continue…-.
Linked Lists Chapter 6 Section 6.4 – 6.6
12 C Data Structures.
Data Structure Interview Question and Answers
12 C Data Structures.
Elementary Data Structures
Chapter 15 Recursion.
CISC181 Introduction to Computer Science Dr
Chapter 5 - Functions Outline 5.1 Introduction
Introduction to C++ Recursion
Introduction to Data Structures
Functions.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 16-2 Linked Structures
Chapter 14 - Advanced C Topics
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
Review & Lab assignments
Chapter: 7-12 Final exam review.
Chapter 17: Linked Lists.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

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

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;

5.13 Recursion

fig05_14.c (Part 1 of 2)

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

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

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 +  

fig05_15.c (Part 1 of 2)

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

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 ) = 832040 Enter an integer: 35 Fibonacci( 35 ) = 9227465 Program Output (continued)

5.14 Example Using Recursion: The Fibonacci Series

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)

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"

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.

fig14_03.c (Part 2 of 2)

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

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

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

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

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

12.4 Linked Lists

fig12_03.c (Part 1 of 8)

fig12_03.c (Part 2 of 8)

fig12_03.c (Part 3 of 8)

fig12_03.c (Part 4 of 8)

fig12_03.c (Part 5 of 8)

fig12_03.c (Part 6 of 8)

fig12_03.c (Part 7 of 8)

fig12_03.c (Part 8 of 8)

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)

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)

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)

12.4 Linked Lists