Write a recursive Ce program to sobve thefollowing problem : Problem 4a P(1) 3 P
ID: 3879296 • Letter: W
Question
Write a recursive Ce program to sobve thefollowing problem : Problem 4a P(1) 3 P(2) = 4 P(n) P(n-1)+P(n-2) for n>2 To test-output for P(4)-11 P(6) 29 and P(7)-47 Hint: main shall call a function int par(int n) that returns 3 if n=1, it returns 4 if n = 2, and it returns par(n-1)+par(n-2) if n>2 Provide screen of your output for n-5 or P/5 Sample Output (Your name will replace mine & yours will be P(5) Cindy shows P(7) has 47 units Problem 4b: Note: the numbers just above the ovals are the return values. You may hand draw&scan; or use Paint or Visio or Googledraw, etc to create this and paste into your document. Draw a call tree for P(S). This should look similar to Figure 2.30 in the textbook.Explanation / Answer
#include <iostream>
using namespace std;
string NAME="Nash"; // replace it with your name
// taking an input parameter n
int par(int n)
{
// base case for n = 1
if(n==1)
return 3;
// base case for n = 2
if(n==2)
return 4;
// if neither of above, then will call it recursively with n-1 and n-2
return par(n-1)+par(n-2);
}
int main() {
// output
cout << NAME << " shows p(5) has " << par(5) << " units";
}
/*OUTPUT
Nash shows p(5) has 18 units
*/
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.