TAILIEUCHUNG - 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
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.