CS148 Introduction to Programming II

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
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.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
1 Storage Classes, Scope, and Recursion Lecture 6.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Sequential & Object oriented Programming
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Hopefully this lesson will give you an inception of what recursion is.
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.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
C++ Programming Lecture 12 Functions – Part IV
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
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 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Program Development and Design Using C++, Third Edition
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CS212: Data Structures and Algorithms
Sections 4.1 & 4.2 Recursive Definitions,
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
C Functions -Continue…-.
CS149D Elements of Computer Science
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CS149D Elements of Computer Science
CS170 Computer Organization and Architecture I
Recursion Chapter 11.
CS1120: Recursion.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS148 Introduction to Programming II
Module 1-10: Recursion.
CS148 Introduction to Programming II
CS149D Elements of Computer Science
CSC 143 Recursion.
Yan Shi CS/SE 2630 Lecture Notes
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS148 Introduction to Programming II
CS148 Introduction to Programming II
Presentation transcript:

CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 19: 4/14/2003 Lecture 19: 4/14/2003 CS148 Spring 2003

Outline Recursion Chapter 18 Lecture 19: 4/14/2003 CS148 Spring 2003

What is Recursion? A recursive definition is a definition in which something is defined in terms of smaller versions of itself YN = Y * Y * Y * ….* Y (Y multiplied by itself N times) YN = Y * (Y * Y * Y *…Y) (Y multiplied by itself N-1 times) YN = Y * YN-1 To calculate YN, multiply Y * by the value of YN-1 To calculate YN-1, multiply Y by the value of YN-2 We stop when N is equal to 1, since we know Y1-1 is Y0 = 1 The case for which an answer is explicitly known is called the base case A recursive algorithm is an algorithm that expresses the solution in terms of a call to itself, a recursive call. A recursive algorithm must terminate, i.e., it must have a base case Lecture 19: 4/14/2003 CS148 Spring 2003

A recursion example Power Function Power (2,3) Returns 8 x n 2 3 //calculates xn //n is assumed to be > 0 int Power (int x, int n) { if (n == 1) return x; // base case else return x* Power (x,n-1); // recursive call } Returns 8 x n 2 3 Call 1 Returns 4 x n 2 2 Call 2 Each recursive call to Power can be thought of as creating a completely new copy of the function, each with its own copies of the parameters x and n When a function is called, the computer system creates temporary storage for the parameters and the functions automatic local variables. This temporary storage is a region of memory called the run-time stack Returns 2 x n 2 1 Call 3 Lecture 19: 4/14/2003 CS148 Spring 2003

Another example The factorial of a number N (written N!) is N multiplied by N-1, N-2, N-3, and so on N! = N * (N-1)! 3!= 3*2*1 0! = 1 1! = 1 Factorial (3) Returns 6 //calculates n!, n assumed to be >= 0 int Factorial (int n) { if (n == 0 || n == 1) return 1; // base case else return n* Factorial (n-1); // recursive call } n 3 Call 1 Returns 2 n 2 Call 2 How to write a recursive algorithm? Identify the base case Identify the recursive case(s) Returns 1 n 1 Call 3 Lecture 19: 4/14/2003 CS148 Spring 2003

Yet Another Example Given the following input data (where \n denotes the newline character) ABCDE\n What is the output of the following program? (Question 10, page 1048, chapter 18) void main () { Rev(); } void Rev() char ch; cin.get (ch); if (ch != ‘\n’) cout << ch; Lecture 19: 4/14/2003 CS148 Spring 2003