TAILIEUCHUNG - Absolute C++ (4th Edition) part 57

Absolute C++ (4th Edition) part 57. 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. . | Thinking Recursively 567 is that the chain of recursive calls will always reach a stopping case and that the stopping case always returns the correct value. When designing a recursive function you need not trace out the entire sequence of recursive calls for the instances of that function in your program. If the function returns a value all you need do is check that the following three properties are satisfied 1. There is no infinite recursion. A recursive call may lead to another recursive call and that may lead to another and so forth but every such chain of recursive calls eventually reaches a stopping case. 2. Each stopping case returns the correct value for that case. 3. For the cases that involve recursion If all recursive calls return the correct value then the final value returned by the function is the correct value. For example consider the function power in Display . 1. There is no infinite recursion The second argument to power x n is decreased by 1 in each recursive call so any chain of recursive calls must eventually reach the case power x 0 which is the stopping case. Thus there is no infinite recursion. 2. Each stopping case returns the correct value for that case The only stopping case is power x 0 . A call of the form power x 0 always returns 1 and the correct value for x0 is 1. So the stopping case returns the correct value. 3. For the cases that involve recursion If all recursive calls return the correct value then the final value returned by the function is the correct value The only case that involves recursion is when n 1. When n 1 power x n returns power x n - 1 x To see that this is the correct value to return note that if power x n - 1 returns the correct value then power x n - 1 returns xn-1 and so power x n returns xn-1 x which is xn and that is the correct value for power x n . That s all you need to check to be sure that the definition of power is correct. The above technique is known as mathematical induction a concept that you .

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.