Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions