Square Roots?

This is a discussion on Square Roots? within the C Programming forums, part of the General Programming Boards category; We're supposed to write a program that will approximate a square root using a recursive function. For some reason, I ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22

    Square Roots?

    We're supposed to write a program that will approximate a square root using a recursive function. For some reason, I cannot get it to return the proper numbers. I ran an error check using some printf's and it seems like the lower value is not being replaced when it should be. Can anyone help me out? Here is my function:

    Code:
    double square_root(double low, double high)
    {
        float mid;
        
        mid = (high + low) / 2;
    
        //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);
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I don't understand your logic. Which variable holds the number you are wanting the square root of?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, the question is: once you are inside the function, how do you know what number you're trying to take the square root of? (Answer: you don't, but you really should.)

    I mean, say we're trying to find the square root of 10 and we pass 0, 10 as arguments. Then mid is 5, 5*5 > 10, so we call square_root(0,5) and all of a sudden we're trying to find the square root of 5.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    Oh, sorry. The number is getting passed into the high value. Here is my main:

    Code:
    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;

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You would do good to rename your variables to something like this:

    Code:
    double square_root(double current_guess, const double number);
    or something more meaningful than low and high. Low and high, to me, represent guesses.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MiroMage View Post
    Oh, sorry. The number is getting passed into the high value.
    Yes; well, as we mentioned, it's not going to stay there. You're probably going to need three function parameters if you want to do this recursively.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    Oh wow. Haha. Thanks guys. I can't believe I didn't see that.

    It's been a long night. Thanks again. ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  3. Simplifing Square Roots
    By LiNeAr in forum C++ Programming
    Replies: 11
    Last Post: 10-01-2005, 08:56 PM
  4. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 03:29 AM
  5. Square roots / Powers etc.
    By Robert602 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2001, 03:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21