The mathematical definition and example of a recursive function f is as follows:
ID: 3701660 • Letter: T
Question
The mathematical definition and example of a recursive function f is as follows: ase case f(0) 5 ecursive case: Example f(1) 2(f (n 1))-3 2(f(0)) 3 2(5)-3 7 Consider the following function prototype for calculating f(n) In a C+ program named recursion.cpp, complete the function prototype using recursion. For full credit, the program must include the following: A true recursive structure (including recursive function calls) Display on console output for each iteration of the factorial calculation, including f(0) and f(n) A comment indicating the base case of the function A comment indicating the recursive case of the function Input validation in the main function for integers greater than or equal to 0 A sample run of the program is shown below: Enter a number greater than or equal to 0 1 Please enter a valid number! Enter a number greater than or equal to 0: 8 f (0) 5 E(1) 7 f (2) 11 E (3) 19 (4) 35 E (5) 67 t (6) 131 £ (7) 259 f (8) 515 Press any key to continue . . .Explanation / Answer
#include<iostream>
using namespace std;
int f(int n)
{
int f_n=0;//to store return value
if(n==0)
{
f_n = 5;
}
else
{
f_n += 2*f(n-1)-3;
}
cout<<"f("<<n<<") = "<<f_n<<endl;//printing f(n) after a function call is completed
return f_n;
}
int main()
{
int inp;
cout<<"Enter value greater than or equal to -1 : ";
cin>>inp;
if(inp>=0)
{
f(inp);
}
else
{
cout<<"Please Enter a valid number"<<endl;
}
return 0;
}
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.