Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

New Mexico Computer Science For All
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 6 Questions Quick Quiz
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Visual C++ Programming: Concepts and Projects Chapter 10B: Recursion (Tutorial)
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Topic 5.
Chapter 15 Recursion.
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Recursion Chapter 11.
JavaScript: Functions Part II
Chapter 8: Recursion Java Software Solutions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Java Software Solutions Foundations of Program Design Sixth Edition
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)

Objectives In this chapter, you will: Discover how to recognize a recursive problem Code a recursive method Use recursion to draw fractal images Analyze recursion Create and code menus Programming with Visual C++2

Factorial Numbers Programming with Visual C++3 Factorial numbers – Example: 5! = 5 x 4 x 3 x 2 x 1 – Can be easily computed using a loop

Factorial Numbers (continued) Factorial numbers – Can also be computed as separate steps from the bottom up – Solving the problem from the bottom up will lead us to a new solution strategy called recursion Programming with Visual C++4

Recursion Refers to re-occurring events of a similar nature Mathematical example: factorial numbers Everyday example: a chain of phone calls A problem (finding an apartment) must be solved by a chain of similar events (phone calls) The events repeat themselves until an answer is obtained The answer follows the same path back Programming with Visual C++5

Recursion (continued) Programming with Visual C++6

Recursion (continued) Programming with Visual C++7

Recursion Recursive solutions Have a recursive case A call to continue the same operation at another level Have a base case A stopping point Use backtracking To go back to the previous operation at a higher level Programming with Visual C++8

Recursion (continued) Programming with Visual C++9 Algorithm for computing factorial numbers

Recursion (continued) Programming with Visual C++10

Recursion (continued) Programming with Visual C++11

Recursion (continued) Recursion and factorial numbers – Recursive definition of a factorial number (n!) – For all positive integers n, n! is defined as follows If n = 0 or 1, then n! = 1 (this is the base case) If n > 1, then n! = n x (n-1)! (this is the recursive case) Programming with Visual C++12

Recursion (continued) Programming with Visual C++13 – Recursive definitions can be translated easily into recursive methods Example: Factorial() method

Recursion (continued) Programming with Visual C++14

Recursion (continued) Programming with Visual C++15

Recursion vs. Iteration Iteration is usually faster – Does not require background overhead to keep track of each method call Recursion is more elegant – More closely matches the mathematical definition of the operation it is performing Recursion can more easily solve some problems – Example: generating fractal images Programming with Visual C++16

Recursion vs. Iteration (continued) Programming with Visual C++17

Creating Fractal Images Draw a line – Then draw three lines half the size (at 90 degree angles to one another) from the end of the line you just drew Apply this strategy recursively to every line you draw Do this until you have reached a predetermined stopping point (the base case) Example: recursive crosses Programming with Visual C++18

Creating Fractal Images (continued) Programming with Visual C++19

Creating Fractal Images (continued) Programming with Visual C++20

Creating Fractal Images (continued) Programming with Visual C++21

Creating Fractal Images (continued) Programming with Visual C++22

Creating Fractal Images (continued) Programming with Visual C++23

Creating Fractal Images (continued) Programming with Visual C++24

Computer-Generated Fractal Images Drawing lines with Graphics class method DrawLine() Use DrawLine() to create a line – Parameters Pen x1, y1 (coordinates of the start of the line) x2, y2 (coordinates of the end of the line) Programming with Visual C++25

Computer-Generated Fractal Images (continued) The starting coordinates of the line are ( x, y ) The ending coordinates are ( newX, newY ) In a recursive solution, there are many levels of line-drawing tasks The coordinates of the end of the previous line become the coordinates of the starting point of the next one Programming with Visual C++26

Recursive DrawBranch() Method Programming with Visual C++27

Recursive DrawBranch() Method (continued) DrawBranch() is recursive Each level of recursion draws three new lines from the end of the previous one The DrawBranch() method is called three times to do this The base case is the last level of recursion Programming with Visual C++28

Recursive DrawBranch() Method (continued) Programming with Visual C++29

Recursive DrawBranch() Method (continued) Programming with Visual C++30

Recursive DrawBranch() Method (continued) Programming with Visual C++31 Algorithm for method DrawBranch()

Recursive DrawBranch() Method (continued) Programming with Visual C++32 A single branch with five levels of recursion looks like a pattern of crosses You will develop a program in the Tutorial to implement this

Recursive DrawBranch() Method (continued) Programming with Visual C++33

Summary Recursion involves a method calling itself Recursive methods must have: – A base case – A recursive case Recursive backtracking – After the base case is reached, the method returns to the previous one – Backtracking continues until the program returns to the original method call Programming with Visual C++34

Summary (continued) Some problems have easy and elegant recursive solutions – Tasks with mathematically inductive definitions are naturally recursive – Example: factorial numbers – Example: fractal images Programming with Visual C++35