tailieunhanh - Absolute C++ (4th Edition) part 58

Absolute C++ (4th Edition) part 58. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | Answers to Self-Test Exercises 577 cout else stars n - 1 cout 3. using std cout void backward int n if n 10 cout n else cout n 10 write last digit backward n 10 write the other digits backward 4-5. The answer to 4 is writeUp . The answer to 5 is writeDown . include iostream using std cout using std endl void writeDown int n if n 1 cout n write while the recursion winds writeDown n - 1 5 void writeUp int n if n 1 writeUp n - 1 cout n write while the recursion unwinds 578 Recursion testing code for both Exercises 4 and 5 int main cout calling writeUp 10 n writeUp 10 cout endl cout calling writeDown 10 n writeDown 10 cout endl return 0 Test results calling writeUp 10 1 2 3 4 5 6 7 8 9 10 calling writeDown 10 10 9 8 7 6 5 4 3 2 1 6. An error message that says stack overflow is telling you that the computer has attempted to place more activation frames on the stack than are allowed on your system. A likely cause of this error message is infinite recursion. 7. using std cout void cheers int n while n 1 cout Hip n cout Hurray n 8. using std cout void stars int n for int count 1 count n count cout 9. using std cout void backward int n while n 10 Answers to Self-Test Exercises 579 cout n 10 write last digit n n 10 discard the last digit cout n 10. Trace for Self-Test Exercise 4. If n 3 the code to be executed is if 3 1 writeDown 3 - 1 On the next recursion n 2 and the code to be executed is if 2 1 writeDown 2 - 1 On the next recursion n 1 and the code to be executed is if 1 1 writeDown 1 - 1 On the final recursion n 0 and the true clause is not executed if 0 1 condition false this clause is skipped The recursion unwinds the cout n line of code is executed for each recursive call that was on the stack with n 3 then n 2 and finally n 1. The output is 3 2 1. 11. Trace for Self-Test Exercise 5. If n 3 the code to be executed is if 3 1 cout 3 writeUp 3 - 1 On the next recursion n 2 and the code to be executed is if 2 1 cout 2 writeUp 2 - 1

TỪ KHÓA LIÊN QUAN