Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Advertisements

CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Exercise 10 Review: pointers, strings and recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
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.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Recursion.
Command Line Arguments
Programmazione I a.a. 2017/2018.
Recursion Chapter 11.
Recursion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 18 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion

7-2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion – The process of solving a problem by reducing it to smaller versions of itself – Example: Factorial

7-3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive algorithm – An algorithm is called recursive if it solves a problem by reducing the problem to smaller versions of itself – A recursive algorithm can be implemented using a recursive function

7-4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive function – A function that calls itself – What is the danger? – we need to avoid infinite recursion – A recursive function must have – recursive call(s) to the function with a smaller instance of the problem – one or more base cases to stop recursion

7-5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding a recursive solution – A recursive solution to a problem must be written carefully – The idea is for each successive recursive call to take us one step closer to the base case (a situation in which the problem can easily be solved)

7-6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley General format for many recursive functions if (some easily-solved condition) // base case solution statement(s); else // general case recursive function call(s);

7-7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example – Write a recursive function Fact() to find the factorial of n. – What should Fact(4) return? – What can be a good base case that represents an easily-solved situation? – What can be a good general case that takes us closer to the base case?

7-8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example int Fact (int n) { if (n == 1) // base case return 1; else // general case return n * Fact(n-1); } // this is an example of tail recursion

7-9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example – Trace of calls n = 4 n = 3 n = 2 Call 1: Fact(4) Call 2: Fact(3) n = 1 Call 3: Fact(2) Call 4: Fact(1) n = 1 Returns 1 Returns 2 * Fact(1) = 2 * 1 = 2 Returns 3 * Fact(2) = 3 * 2 = 6 Returns 4 * Fact(3) = 4 * 6 = 24

7-10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Assignment – Write a recursive function Power() to calculate x n, where n is a non-negative integer. – Extend the Power() function so that it works for any integer n.

7-11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Empty base case – Recursive functions can have empty base cases – What action should take place? – What should be the return type? – Write a recursive function that will display all integers from n down to 1.

7-12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Iteration or Recursion? – Key factors – nature of the problem – efficiency – Every recursive call has its own set of parameters and local variables – time and space overhead associated with executing a recursive function – Certain problems are inherently recursive and a recursive solution is the most natural one

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments

7-14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments – Arguments can be passed to any program being executed from a command-line based OS like DOS or UNIX – UNIX Example: cp fileA fileB – cp is the name of the program (command) to be executed – fileA and fileB are command-line arguments – In C/C++, the program can be written to accept command-line-arguments

7-15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C/C++ syntax – The following syntax is used in the main() function to capture command-line arguments int main(int argc, char *argv[]) – In this case, the operating system passes in two parameters to the function – argc is an integer specifying the number of command- line arguments – argv is an array of pointers where each pointer points to one of the command-line arguments stored as a C- type string

7-16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C++ syntax (Cont..) – In some systems (like UNIX), argv[0] points to a string that is the entire path to the program (executable) including its name – In other systems, argv[0] points to garbage – You can use any argv[i] like a C-type string – In C++, if the program is not written to accept command-line arguments, it is not necessary to specify the parameters in the argument list of the function signature

7-17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example #include using namespace std; void main(int argc, char *argv[]) { string fname, lname; fname = argv[1]; lname = argv[2]; cout << argc << " " << argv[1] << " " << argv[2] << endl; } g++ -o comlinarg comlinarg.cpp./comlinarg John Malkovich 3 John Malkovich