《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance

Chapter 13 ABSOLUTE C++ Recursion WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 13 Recursion

Learning Objectives Recursive void Functions Tracing recursive calls Infinite recursion,overflows Recursive Functions that Return a value ◆Powers function Thinking Recursively Recursive design techniques ◆Binary search Copyright006 Pearson Addison-Wesley.All rights reserved. 13-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-2 Learning Objectives ¨ Recursive void Functions ¨Tracing recursive calls ¨Infinite recursion, overflows ¨ Recursive Functions that Return a Value ¨Powers function ¨ Thinking Recursively ¨Recursive design techniques ¨Binary search

Introduction to Recursion A function that "calls itself" ◆Said to be recursive In function definition,call to same function ◆C++allows recursion As do most high-level languages Can be useful programming technique ◆Has limitations Copyright 2006 Pearson Addison-Wesley.All rights reserved. 13-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-3 Introduction to Recursion ¨ A function that "calls itself" ¨Said to be recursive ¨In function definition, call to same function ¨ C++ allows recursion ¨As do most high-level languages ¨Can be useful programming technique ¨Has limitations

Recursive void Functions ◆Divide and Conquer Basic design technique Break large task into subtasks Subtasks could be smaller versions of the original task! ◆Vhen they are→recursion Copyright006 Pearson Addison-Wesley.All rights reserved. 13-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-4 Recursive void Functions ¨ Divide and Conquer ¨Basic design technique ¨Break large task into subtasks ¨ Subtasks could be smaller versions of the original task! ¨When they are recursion

Recursive void Function Example ◆Consider task: Search list for a value Subtask 1:search 1st half of list Subtask 2:search 2nd half of list Subtasks are smaller versions of original task! When this occurs,recursive function can be used. Usually results in "elegant"solution Copyright 2006 Pearson Addison-Wesley.All rights reserved. 13-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-5 Recursive void Function Example ¨ Consider task: ¨ Search list for a value ¨ Subtask 1: search 1st half of list ¨ Subtask 2: search 2nd half of list ¨ Subtasks are smaller versions of original task! ¨ When this occurs, recursive function can be used. ¨ Usually results in "elegant" solution

Recursive void Function: Vertical Numbers Task:display digits of number vertically, one per line ◆ Example call: writeVertical(1234); Produces output: 1 2 3 4 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 13-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-6 Recursive void Function: Vertical Numbers ¨ Task: display digits of number vertically, one per line ¨ Example call: writeVertical(1234); Produces output: 1 2 3 4

Vertical Numbers: Recursive Definition Break problem into two cases Simple/base case:if n=10,two subtasks: 1-Output all digits except last digit 2-Output last digit ◆ Example:argument 1234: 1st subtask displays 1,2,3 vertically 2nd subtask displays 4 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 13-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-7 Vertical Numbers: Recursive Definition ¨ Break problem into two cases ¨ Simple/base case: if n=10, two subtasks: 1- Output all digits except last digit 2- Output last digit ¨ Example: argument 1234: ¨ 1st subtask displays 1, 2, 3 vertically ¨ 2nd subtask displays 4

writeVertical Function Definition Given previous cases: void writeVertical(int n) if(n<10) //Base case cout <n <endl; else { //Recursive step writeVertical(n/10); cout <<(n%10)<<endl; Copyright006 Pearson Addison-Wesley.All rights reserved. 13-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-8 writeVertical Function Definition ¨ Given previous cases: void writeVertical(int n) { if (n < 10) //Base case cout << n << endl; else { //Recursive step writeVertical(n/10); cout << (n%10) << endl; } }

writeVertical Trace ◆Example call: writeVertical(123); writeVertical(12);(123/10) →writeVertical(1);(12/10) →cout<<1<<endl; cout <2 <endl; cout <3 <endl; Arrows indicate task function performs Notice 1st two calls call again (recursive) Last call (1)displays and "ends" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 13-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-9 writeVertical Trace ¨ Example call: writeVertical(123); writeVertical(12); (123/10) writeVertical(1); (12/10) cout << 1 << endl; cout << 2 << endl; cout << 3 << endl; ¨ Arrows indicate task function performs ¨ Notice 1st two calls call again (recursive) ¨ Last call (1) displays and "ends

Recursion-A Closer Look Computer tracks recursive calls Stops current function Must know results of new recursive call before proceeding Saves all information needed for current call ◆To be used later Proceeds with evaluation of new recursive call When THAT call is complete,returns to "outer"computation Copyright 2006 Pearson Addison-Wesley.All rights reserved. 13-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 13-10 Recursion—A Closer Look ¨ Computer tracks recursive calls ¨ Stops current function ¨ Must know results of new recursive call before proceeding ¨ Saves all information needed for current call ¨ To be used later ¨ Proceeds with evaluation of new recursive call ¨ When THAT call is complete, returns to "outer" computation
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding A.pdf
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions.ppt
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)3-a-b-Computer Software.pdf
- 《计算机导论》课程教学课件(英文讲稿)4- operating system.pdf
- 《计算机导论》课程教学课件(英文讲稿)4-a- File mangement.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-a- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-b- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-a- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-b- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)7-a- Web.pdf