I am trying to create a recursive function:

double fifthRoot(double x, double maxError)

In this function, I have to return y such that y^5 will be within maxError of x. Assume x > 0. Also assume maxError is big enough to prevent problems due to rounding.

In order to make this function work, I have to call a helper function that takes the original number x, maxError and two doubles, e.g. low and high. low is a number whose fifth power is <= x an high's fifth power is >= x. Take the average of those two numbers and take the fifth power of that. Then update low and high appropriately.

Here is my code:

double fifthRootHelper(double x,  double maxError, double low, double high)
{
    double average = (high + low) / 2;
    double fifthPower = (average * average * average * average * average);
    if((fifthPower - x) <= x)
    {
        return fifthRootHelper(x,maxError,low,fifthPower);
    }
    if((-fifthPower - x) >= x)
    {
        return fifthRootHelper(x,maxError,fifthPower,high);
    }
    return fifthPower - x;
}

double fifthRoot(double x, double maxError)
{
    double maximum = (x <= maxError || x >= maxError) ? maximum : 1;
    return fifthRootHelper(x,0,maximum,maxError);
}

int main()
{   
    double num = 32;
    double error = 1;
    cout << fifthRoot(num,error);
    return 0;
}

When I ran this code, my program crashed. Is there anything that I need to change in my fifthRoot() and/or helper functions?

Line 18 of what you posted makes no sense. It will always select maximum = maximum in the trinary operator and you cannot declare a variable equal to an undeclared variable (even if you could it would be holding a garbage value).

Line 19 you are now saying that the low guess is equal to some mystery value and your high guess is the maximum tolerated error? and then your maxError is now set to 0 and never used within your helper function.

In your helper function the only two lines that make sense are the first two where you calculate the average and the resulting fifth power of that value. Other than that I do not fully understand what you are trying to do because fifthPower - x will not give anything useful the way you are using it (maybe if you used maxError in here somewhere then it would make sense, but then again you are setting that to 0.0).

Here is what I think you are attempting to do

#include <iostream>
#include <cmath>

using namespace std;

double fifthRootHelper(double x, double maxError, double low, double high)
{
    if (x == 1.0) //special case for 1.0
        return 1.0;

    double avg = (low + high) / 2.0;  //use 2.0 not 2 just for the future case of running into integer division without knowing
    double fifthPower = avg*avg*avg*avg*avg;

    if (fabs(fifthPower - x) < maxError) //if our new guess is within our error, return that as the answer
        return avg;

    if (fifthPower > x) //if our guessed value is too high then set that to our upper bound
        high = avg;
    else //if it is too low then make it our lower bound
        low = avg;

    return fifthRootHelper(x, maxError, low, high); //recalc
}

double fifthRoot(double x, double maxError)
{
    return fifthRootHelper(x, maxError, 0.0, x); //assume the lower bound is 0.0 and the upper bound is the value being passed
}

int main()
{

    double num = 32.0;
    double error = 1.0;

    cout << fifthRoot(num, error) << endl;


    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.