C++ MyProgrammingLab Problem Define a recursive function double root( double x,
ID: 3562417 • Letter: C
Question
C++ MyProgrammingLab Problem
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.
Do not write the entire program.
Explanation / Answer
By using Newton-Raphson Root Finding Algorithm:
double fun1(double ar, double guess,int n) {
return (Math.pow(guess,n) - ar);
}
double fun2(double guess,int n) {
return (n * Math.pow(guess, n-1));
}
boolean appoximate(double first, double second) {
return (Math.abs(first-second) < Math.abs(second * 0.0001));
}
double getRoot(double x, int n, int lower,int upper,double guess) {
double newGuess = guess - f(a,guess,n) / fun2(guess,n);
if (appoximate(newGuess, g))
return newGuess;
else
return getRoot(w, newGuess, n);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.