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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started