Given the following recurrence relation of a sequence, write a function that ret
ID: 3699552 • Letter: G
Question
Given the following recurrence relation of a sequence, write a function that returns the n-th term of the sequence, where n is input by users. Print out the value in the console. 1. an-an-1/2+2an-2;a 0, a 2 NOTE: 1. You should implement in recursion. Do NOT use for loop. Function template is int YourFuntionName(int n)l 1. Initialize the starting values (i.e. ao and a1) 2. Return the base case n-0 or 1, return ao or a, 3. If not the base case: return the recursive call of the function options compilation execution Please input an integer: For n-3, an = 4.5Explanation / Answer
#include<iostream>
using namespace std;
//given reccurence relation
//an = an-1/2 + 2*an-2
//a0= 0
//a1 = 2
int reccurence_relation(int n)
{
if(n==0)//base case
return 0;//a0= 0
if(n==1)
return 2;//a1 = 2
return reccurence_relation(n-1)/2 + reccurence_relation(n-2)*2 ;////an = an-1/2 + 2*an-2
}
//testing method
int main()
{
int n;
cout<<"Enter number:";
cin>>n;
cout<<reccurence_relation(n)<<endl;
return 0;
}
output:
Enter number:5
10
Process exited normally.
Press any key to continue . . .
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.