Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

language is c++ and need to run it in code blocks Recursion Recall from the reading that recursion is the use of a function that

language is c++ and need to run it in code blocks

Recursion

Recall from the reading that recursion is the use of a function that calls itself to solve some problem. Recursion implements a looping mechanism so you do not usually put a looping structure (while, do-while, or for) in a recursive function. In fact, in this course you never want to put a loop in a recursive function. Instead you will use if-else (or if-else if) statements. Problems that can be solved recursively have two parts, the base case and the recursive step (or recursive case). A base case can be thought of as a version of a problem for which the answer is known. The recursive step is what you can do to make the problem a little easier to solve (and if you do it enough times eventually you get to a base case). In a recursive function you use if statements to describe what to do for each base case and recursive step. Start by writing a recursive function to find the factorial of a positive integer (recall that the factorial of a positive integer is the number multiplied with all integers between itself and 1; for example 5! = 5 * 4 * 3 * 2 * 1 = 120). For this problem, the base cases are 1 and 0. For both of these values the answer is just defined to be 1. Assume your function begins as follows: int factorial(int x) Your function should contain an if-else if to determine if the parameter X is one of the two base cases, and if so, the function should return the appropriate value. For this problem, the recursive step is X * (X-1)! It might not be immediately obvious why this is the recursive step, but the more practice you get with recursion, the clearer this will become. Your function should end with an else that applies the recursive step as follows: return X * factorial(X-1); essentially, this says that the factorial of X can be found by multiplying X by the factorial of X 1. It might seem strange that the function contains a call to itself, but this is exactly what recursion is all about. Once you have your recursive function written write a short main function to test it out and make sure that it works. Now, assume we have the following call to your factorial function: cout << factorial(3) << endl; this should print the number 6 to the screen. With your group, draw a picture (similar to the one shown in Figure 7.2.1 of the zyBook) that shows the series of stack frames that would get produced for the call factorial(3). This will help to formalize your understanding of how recursion works. What happens if you call your function with factorial(-5)? Can you fix the function so that it works correctly? Now write a recursive function called sumTo0 that returns the sum of all positive integers between the parameter and 0. For example, sumTo0(4) returns 10, and sumTo0(-3) returns -6. For this lab activity you need to submit both your .cpp file with the code for your factorial function and a Word document with your picture that shows the stack frames that get produced for the call factorial(3).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

Students also viewed these Databases questions