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

184.Define a recursive function double root(double x, int n, double lower, doubl

ID: 3763652 • Letter: 1

Question

184.Define a recursive function

double root(double x, int n, double lower, double upper);

that finds the nth root of x to at least 5 digits of precision.

As a precondition, the parameters lower and upper are such that lower^n <= x and upper^n >=x.

You may want to define a recursive a helper function to compute nth powers.

Hint: try a divide and conquer approach similar to the code for square root.

Use only basic operations +, -, * in your code; no <cmath> or other library functions.

Driver code:
cout << fixed << showpoint << setprecision(5);
double d;
int n;
cin >> d >> n;
cout << root(d,n,0, d<1?1:d);

Explanation / Answer

#include<iostream.h>

double root(double x , int n, double Low ,double High )
{

const double sqrt_mef = 1e-5; // Approximate square root of machine efficiency

double mid = (Low + High)/2; // No +1

if((High-Low)/(1+mid) < sqrt_mef)
{
return mid;
}

else if (N < mid * mid )
{
return root(x,n,Low,mid) ; // No -1
}
else
{
return root(x,n,mid,High) ;
}

}

void main()
{
   cout << fixed << showpoint << setprecision(5);
   double d;
   int n;
   cin >> d >> n;
   cout << root(d,n,0, d<1?1:d);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote