CS 202 Computer Science II Lab Fall 2009 October 29.

Slides:



Advertisements
Similar presentations
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Advertisements

1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: Parameter.
CS 202 Computer Science II Lab Fall 2009 November 5.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Function Recursion Examples “an example is worth a 1000 lectures”
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
© 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.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Lecture 3.4: Recursive Algorithms CS 250, Discrete Structures, Fall 2011 Nitesh Saxena *Adopted from previous lectures by Zeph Grunschlag.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Review of Recursion For those of you that have slept since.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
R. Johnsonbaugh, Discrete Mathematics 5 th edition, 2001 Chapter 3 Algorithms.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Lecture 3.4: Recursive Algorithms CS 250, Discrete Structures, Fall 2015 Nitesh Saxena Adopted from previous lectures by Zeph Grunschlag.
Recursion Function calling itself
CS212: Data Structures and Algorithms
Programming and Data Structures
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter Topics Chapter 16 discusses the following main topics:
Recursion CSE 2320 – Algorithms and Data Structures
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Introduction to C++ Programming Language
Topic 6 Recursion.
Greatest Common Divisor
Greatest Common Divisor
Chapter 15 Recursion.
Chapter 15 Recursion.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion CSE 2320 – Algorithms and Data Structures
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 14: Recursion Starting Out with C++ Early Objects
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursion Chapter 18.
Recursive GCD Demo public class Euclid {
Module 1-10: Recursion.
Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Main() { int fact; fact = Factorial(4); } main fact.
Handout-16 Recursion Overview
Introduction to Algorithms
Presentation transcript:

CS 202 Computer Science II Lab Fall 2009 October 29

Today Topics Makefile Recursion

Makefile main.out: main.o stack$(VAL).o g++ -o main.out main.o stack$(VAL).o main.o: main.cpp stack$(VAL).h g++ -c main.cpp Stack$(VAL).o: stack$(VAL).cpp stack$(VAL).h g++ -c stack$(VAL).cpp $ make VAL=1 $ make VAL=2

Recursion –A method of specifying a process by means of itself. Recursive Function –A way of defining a function using recursion. Example F(x) = 30x + 7 Direct Implementation F(x) = F(x-1) - 30 Recursion Implementation

Recursion Base Case of a Recursively Defined Function – A value or values for which the recursive function is concretely defined. xF(x) F(x) = 30x + 7 Base case: F(0) = 7

Recursion Recursive Algorithm: – An algorithm that calls itself with smaller and smaller subsets of the original input, doing a little bit or work each time, until it reaches the base case. int F(int x) { if (x=0) return 7; else return F(x-1) + 30; } int F(int x) { return (x= =0) ? 7 : F(x-1)+30; }

Recursion Example factorial(n) n! = nx(n-1)x(n-2)x…x2x1 Direct Imp. n! = nx(n-1)! Recursive Imp. Base case: If n = 0 then factorial(n) = 1 int factorial(int n) { if (n>0) return n*factorial(n-1); else return 1; } int factorial(int n) { return (n) ? n*factorial(n-1) : 1; }

Recursion Example Fibonacci numbers A sequence of numbers named after Leonardo of Pisa, known as Fibonacci. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 … If n = 0 thenfib(n) = 1 If n = 1 thenfib(n) = 1 If n > 1 thenfib(n) = fib(n - 1) + fib(n - 2)‏

Recursion Example Fibonacci numbers int fib(int n) { if (n==0 or n==1) { return 1; } else { return fib(n-1)+fib(n-2); } }

Recursion Example The greatest common divisor (GCD) of two integers m and n int tryDivisor(int m, int n, int g){ if ( ( (m % g) == 0 ) && ( (n % g) == 0 ) ) return g; else return tryDivisor(m, n, g - 1); } int gcd(int m, int n) { return tryDivisor(m, n, n); //use n as our first guess } gcd(6, 4) tryDivisor(6, 4, 4) tryDivisor(6, 4, 3) tryDivisor(6, 4, 2) => 2

GCD int gcd(int m, int n) { if(m == n) return m; elseif (m > n) return gcd(m-n, n); else return gcd(m, n-m); } gcd(468, 24) gcd(444, 24) gcd(420, 24)... gcd(36, 24) gcd(12, 24) (Now n is bigger) gcd(12, 12) (Same) => 12

Extremely Smart way to compute GCD int gcd(int m, int n) { if ( (m % n) == 0 ) return n; else return gcd(n, m % n); } gcd(468, 24) gcd(24, 12) => 12 gcd(135, 19) gcd(19, 2) gcd(2, 1) => 1

Questions?