here is the code that I have so far and I can not seem to get it to work properly it is return 1 for any number that is entered. Any help that you can give me would be greatly appreciated. Thanks

#include <stdio.h>



double square_root(double low, double high);

int main(void)
{
    float number;
    double root;
    
    printf("Enter a number to compute the square root of.\n");
    scanf("%f", &number);
    
    root = square_root(1, number);
    
    printf("The square root of %.3f is about %.3lf\n", number, root);
    
    system("PAUSE");
    return 0;
}

	double square_root(double low, double high)
{
    float mid;
    
    mid = (high + low) / 2;
	//x(n+1) = 1/2( x(n) + a / x(n) )

    //Base Case
    if((mid*mid) == high)
        return mid;

     //Stops when the high and low numbers are within 0.001 of one another.   
    if((high - low) < 0.001)
        return mid;
    
    if((mid*mid) > high)
        //return square_root(low, mid);
        
    return square_root(mid, high);
}
Salem commented: Congrats on using code tags correctly on your first post +22

Recommended Answers

All 7 Replies

I took the commit out


mid = (high + low) / 2;

//x(n+1) = 1/2( x(n) + a / x(n) )// I know this is the formula that makes this work I just dont know how to impliment it

And what about the other line of commented out code further on?

Your lack of { } on your if statements is hurting you when you comment out code. It doesn't just remove that line, it changes the behaviour of the code which follows it.

what is your suggestion to solving this, I am lost completely and the more that I try to understand it the more confused that I get. I dont understand what you mean by my lack of {} if you could please elaborate on this I would really appreciate it.
When I enter a number to get the square root of it returns that number not the sq root

Your original code

if((mid*mid) > high)
        return square_root(low, mid);
        
    return square_root(mid, high);

If the test is true, you return the square root between low and mid, otherwise you return the square root between mid and high

Your commented code

if((mid*mid) > high)
        //return square_root(low, mid);
        
    return square_root(mid, high);

Which is really the same as this

if((mid*mid) > high)
        return square_root(mid, high);
    return ?;

Not only do you calculate the square root on the wrong half of the bisection, you return some garbage value if the test fails. I rather suspect that that is where your constant result is coming from.

When I mean add more { }, I mean like this

if((mid*mid) > high) {
        //return square_root(low, mid);
    }        
    return square_root(mid, high);

Now, whether you comment the line out (or not), the next statement will not be called instead simply because the test still passes.

It will on the other hand be called all the time (which is a slightly different problem), because there is now a return statement which is missing in the code.

In fact, adopting a single exit point approach means you're unlikely to unexpectedly fall off the end of a function because you forgot to take into account a possible return path. Seeing the "error" result in your code would alert you to the problem, rather than be left wondering what a particular "junk" result meant.

double square_root(double low, double high)
{
    double mid, result = 0.0;
    
    mid = (high + low) / 2;

    //Base Case
    if((mid*mid) == high) {
        result = mid;
    }
    else if((high - low) < 0.001) {
        //Stops when the high and low numbers are within 0.001 of one another.   
        result = mid;
    }
    else if((mid*mid) > high) {
        result = square_root(low, mid);
    } else {
        result = square_root(mid, high);
    }
    return result;
}

If you comment out any result = line, then you're going to get 0 returned in some case or other.

I really do appreciatte you walking me through this, now that I have implemented all your suggestions it is return the sq root of 4.000 is 1.000, do you have any ideas. I think that it is in the math part: root = (low + high)/2. I think that it needs to be this formula written recursively in code

you need a constant to compair the mid to. add an int to the function. low high constant

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.