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
icon
Related questions
Question
Program #2 Define a sequence of numbers recursively
Define a sequence ao, a1, az, a3, where
ao = 1
an = (an -1+ 1) * 2
an = (2* an - 2+ an-1) ifn is even
So for instance:
(n = 0) ao = 1
(n = 1) ai = (ao + 1) *2 = (1+ 1) *2= 4
(n = 2) az = (2* ao + ao) = 2*1+4 = 6
(n = 3) az = (az + 1) *2 = (6 + 1) *2 =14
The resulting sequence is 1, 4, 6, 14, .
if n is odd
!3!
%3D
You will write a function that returs 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 terms> 4
Term #0> 1
Term #1> 4
Term #2> 6
Term #3> 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
Transcribed Image Text:Program #2 Define a sequence of numbers recursively Define a sequence ao, a1, az, a3, where ao = 1 an = (an -1+ 1) * 2 an = (2* an - 2+ an-1) ifn is even So for instance: (n = 0) ao = 1 (n = 1) ai = (ao + 1) *2 = (1+ 1) *2= 4 (n = 2) az = (2* ao + ao) = 2*1+4 = 6 (n = 3) az = (az + 1) *2 = (6 + 1) *2 =14 The resulting sequence is 1, 4, 6, 14, . if n is odd !3! %3D You will write a function that returs 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 terms> 4 Term #0> 1 Term #1> 4 Term #2> 6 Term #3> 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Computational Systems
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning