Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//add introductory comments # include <iostream> using namespace std; // add com

ID: 666419 • Letter: #

Question

//add introductory comments

# include <iostream>
using namespace std;


// add comments for this function
double RFunct(double x, int n)
{
   double ans;
   if (n == 1)
   {
       ans = x;
   }
   else
   {
       ans = n * x * RFunct(x, n - 1);
   }
   return ans;
}

//Add comments for your function that uses a loop
// Add a function that will perform the same calculation using a loop rather than recursion


int main()
{
   double P1, P2, xinput = 2;
   int num = 5;

   cout << "Enter a number ";
   cin >> xinput;
   cout << xinput << endl;

   cout << "Enter a whole number ";
   cin >> num;
   cout << num << endl;

   P1 = RFunct(xinput, num);
// add function call to your function that uses a loop rather than recursion


   cout << "Result of the recursive function is " << P1;
   cout << " Result of the repetitive function is " << P2 <<endl;
   return 0;
}

Problem: Review the above code. Determine what mathematical formula the function calculates. Copy the code to ideone.com (have it set to C++14 beforehand), add comments for the overall program and for the recursive function given. Add a new function (along with comments for the new function) that will perform the same utility as the given function (you should now have 2 functions other than main), but uses a loop rather than recursion. Add a function call to your new function. Output the result from both functions. Your output should look something like

Enter a number 2.33

Enter a whole number 5

Result of the recursive function is 8240.64

Result of the repetitive function is 8240.64

(You will not see what is in bold, I added it so you could see the input used to generate the answer to the calculation)

Explanation / Answer


# include <iostream>
using namespace std;

//this program is used to find out n! * x^n
// here x is a double
// n is an intiger >=1
/*

in recursive me thod to find n!*x^n = n*(n-1)! * x * x^n-1 = n*x * (n-1)! * x^(n-1)
hence we will find n*x then we will find (n-1)! * x^(n-1) using reccursive call
*/
double RFunct(double x, int n)
{
double ans;
if (n == 1)// base case when n==1 return n*x = 1*x = c
{
ans = x;
}
else
{
ans = n * x * RFunct(x, n - 1);//in each recursive call we will find n*x
//and we will call the same function for x,n-1
}
return ans;
}

//n!*x^n = (n*x) * (n-1 * x) * ( n-2 * x) * ....... * ( 1 * x)
double RFunct_iter(double x, int n)
{
double ans=1;
while(n>0)
{
ans*=n*x;
n--;
}
return ans;
}


int main()
{
double P1, P2, xinput = 2;
int num = 5;
cout << "Enter a number ";
cin >> xinput;
cout << xinput << endl;
cout << "Enter a whole number ";
cin >> num;
cout << num << endl;
P1 = RFunct(xinput, num);
P2 = RFunct_iter(xinput,num);


cout << "Result of the recursive function is " << P1;
cout << " Result of the repetitive function is " << P2 <<endl;
return 0;
}