Implement a recursive function of the signature: int recursiveA(int m, int n); w
ID: 3621484 • Letter: I
Question
Implement a recursive function of the signature:
int recursiveA(int m, int n);
whose return value on input (m,n) is defined by the following mathematical recursion:
recursiveA(m,n) = {n+1, if m=0,
{recursiveA(m-1,1), if m>0 and n=0,
{recursiveA(m-1,recurisveA(m,n-1)) if m>0 and n>0.
Write a program in c++ that can be called with two non-negative integer values m and n as parameters (or it asks the user to enter such number). The output of your program should consist of (i) the value of recursiveA(m,n), (ii) the number of times recursiveA was called in order to compute recursiveA(m,n), and (iii) the maximal recursion depth (ie, the maximal number of activation records of recursiveA on the runtime stack) obtained while computing recursiveA(m,n). You may assume that the input values given to your program are always non-negative integers.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int maxstack;
int entries;
int recursiveA(int,int);
int main()
{maxstack=0;
entries=0;
cout<<"The answer is "<<recursiveA(3,5)<<endl;
cout<<"Entries into function: "<<entries<<endl;
cout<<"Max stack size: "<<maxstack<<endl;
system("pause");
return 0;
}
int recursiveA(int m,int n)
{entries++;
if(m==0)
{maxstack--;
return n+1;
}
if(n==0)
{maxstack++;
return recursiveA(m-1,1);
}
else
{maxstack+=2;
return recursiveA(m-1,recursiveA(m,n-1));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.