Program #2 Define a sequence of numbers recursively Define a sequence as, at, az, as, where ao = 1 an = (an-1+ 1) 2 an = (2* an - 2 + aa -1) ifn is even So for instance: (n = 0) ao = 1 (n = 1) a; = (ao + 1) * 2 = (1+ 1)*2= 4 (n = 2) az = (2* ao + ao) = 2*1+4 = 6 (n = 3) as = (az + 1) *2= (6+ 1) * 2 =14 The resulting sequence is 1, 4, 6, 14, - %3D if n is odd %3D You will write a function that returis the nth term of the sequence. You must do this by using a recursive function. The function will NOT print out any values. Rather, the function will return the nth term of the sequence using a recursive algorithm. In the main part of the program: 1. Input the number of terms of the sequence to output. 2. In a for loop, call the function repeatedly to get the desired number of terms. The function will take i (assuming the for index is called i) as the argument and return the ith term of the sequence. 3. In the for loop, print out each term as it is returned. Figure out the necessary prompts for the inputs and the desired outputs by looking at this example session. The number in red is a possible input and is not what you print out. Enter the number of terma> 4 Term 10> 1 Term 1> 4 Term 12> 6 Term 13> 14 HINTS: 1. The base case is when n==0 2. In the recursive case you will need to decide if n is odd or even. n is odd if there is a remainder when you divide by two. if (n % 2) !=0): #test for odd Since a nonzero number is true, the above could be shortened to: if (n % 2): #test for odd Either way, else: #must be even
Program #2 Define a sequence of numbers recursively Define a sequence as, at, az, as, where ao = 1 an = (an-1+ 1) 2 an = (2* an - 2 + aa -1) ifn is even So for instance: (n = 0) ao = 1 (n = 1) a; = (ao + 1) * 2 = (1+ 1)*2= 4 (n = 2) az = (2* ao + ao) = 2*1+4 = 6 (n = 3) as = (az + 1) *2= (6+ 1) * 2 =14 The resulting sequence is 1, 4, 6, 14, - %3D if n is odd %3D You will write a function that returis the nth term of the sequence. You must do this by using a recursive function. The function will NOT print out any values. Rather, the function will return the nth term of the sequence using a recursive algorithm. In the main part of the program: 1. Input the number of terms of the sequence to output. 2. In a for loop, call the function repeatedly to get the desired number of terms. The function will take i (assuming the for index is called i) as the argument and return the ith term of the sequence. 3. In the for loop, print out each term as it is returned. Figure out the necessary prompts for the inputs and the desired outputs by looking at this example session. The number in red is a possible input and is not what you print out. Enter the number of terma> 4 Term 10> 1 Term 1> 4 Term 12> 6 Term 13> 14 HINTS: 1. The base case is when n==0 2. In the recursive case you will need to decide if n is odd or even. n is odd if there is a remainder when you divide by two. if (n % 2) !=0): #test for odd Since a nonzero number is true, the above could be shortened to: if (n % 2): #test for odd Either way, else: #must be even
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning