Question: Implement a function double getRoot (double x, int n). The function should return the nth root of x. Use the same algorithm as in the

Implement a function double getRoot (double x, int n). The function should return the nth root of x. Use the same algorithm as in the squareRoot function posted below. The difference is that instead of using mid*mid in the test you'll need mid to the power n in the test. That means you will need to write a helper function to take powers, which should also be recursive.

No use of allowed. No loops.

Upload a .cpp with your getRoot function and a main that calls the function at least once.

double squareRootHelper(double x, double low, double high) {

if (high - low < 0.000001) return low;

double mid = (low + high) / 2.0;

if (mid*mid > x) return squareRootHelper(x, low, mid);

return squareRootHelper(x, mid, high);

}

double squareRoot(double x) {

if (x < 1) return squareRootHelper(x, x, 1);

return squareRootHelper(x, 1, x);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!