Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,555 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,606 people online right now. Registration is fast and FREE... Join Now!




help with Recursive Square Root function? (C++)

 
Reply to this topicStart new topic

help with Recursive Square Root function? (C++), calculate the square root of a number using recursion

soccerfreak
23 Nov, 2007 - 10:54 PM
Post #1

New D.I.C Head
*

Joined: 23 Nov, 2007
Posts: 2


My Contributions
Hey

I am just having a little trouble getting my square root function to work, I have already tried to do it, and cannot figure out why it is not working. It is just returning the value i want to take the square root of. Can anyone help?


CODE
# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;

double MySqrt(double value);
double recurSqrt(double value, double temphigh, double templow);

int main()
{
    cout << "\n------------------------------------------" << endl;
    cout << "Please choose from the following menu:" << endl;
    cout << "1. Display the square roots of numbers between 0.0 and 0.9" << endl;
    cout << "2. Display the square roots of numbers between 0.0 and 1.0" << endl;
    cout << "3. Exit program \n" << endl;
    
    int option;
    cout << "Selected option: ";
    cin >> option;
    
    while(option != 3)                    // loop continues while 2 is not entered, if 2 is entered it ends the program
    {
        switch(option)
        {
            case 1:                
            {
                cout << "Number" << setw(30) << right << "Square root(cmath)" << right << setw(35) << "Square root(recursive)" << endl;
                cout << "************************************************************************" << endl;
                for(double count = 0.0; count <= 0.9; count+= 0.1)
                {
                    cout << setprecision(1) << fixed << count << setw(25) << right << setprecision(4) << fixed << sqrt(count) << setw(35) << MySqrt(count)<< endl;
                }
                break;
            }
            case 2:
            {    
                cout << "Number" << setw(30) << right << "Square root(cmath)" << right << setw(35) << "Square root(recursive)" << endl;
                cout << "************************************************************************" << endl;
                for(double count = 0.0; count <= 10.0; count+= 1.0)
                {
                    cout << setw(4) << right << setprecision(1) << fixed << count << setw(25) << right << setprecision(4) << fixed << sqrt(count) << setw(35) << MySqrt(count)<<  endl;
                }
                break;
            }
            case 3:                
            {
                return 0;    
            }
            default:                
            {    
                cout << "Please enter a valid option" << endl;
                break;
            }
        }

        cout << "\n------------------------------------------" << endl;
        cout << "Please choose from the following menu:" << endl;
        cout << "1. Display the square roots of numbers between 0.0 and 0.9" << endl;
        cout << "2. Display the square roots of numbers between 0.0 and 1.0" << endl;
        cout << "3. Exit program \n" << endl;
    
        cout << "Selected option: ";
        cin >> option;
    }
return 0;
}

double MySqrt(double value)                        
{
    double temphigh;
    double templow;

    if((value >= 0) && (value < 1))                // for values between 0.0 and 0.9
    {
        temphigh = value;
        templow = 0;
        return recurSqrt(value, temphigh, templow);        
    }
    else                                        // for values between 1.0 and 10.0
    {
        temphigh = value;
        templow = 1;
        return recurSqrt(value, temphigh, templow);                
    }
}

double recurSqrt(double value, double temphigh, double templow)
{
    double error;
    double guess;

    guess = (templow + temphigh)/2;
    error = value - (guess*guess);
    
    if((error*-1) > 0.00001)
    {
        if(error < 0)                                        // computes if previous iteration was too high
        {    
            temphigh = guess;
            return recurSqrt(value, temphigh, templow);
            
        }
        else                                                // computes if previous iteration was too low
        {
            templow = guess;
            return recurSqrt(value, temphigh, templow);
        }
    }
    else
    {
        return value;            // square root of 0 or 1 is itself
    }
return guess;
}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Help With Recursive Square Root Function? (C++)
24 Nov, 2007 - 10:48 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
ever hear about something called the "principle of the excluded third" sometimes known as "The law of the excluded middle"?

lets look at a simple example:
CODE
int func(int a, int b) {
    if (a>b) {
        return a;
    } else {
        return b;
    }
    return a*b; //this never happens!
}

the function never returns a*b because either a > b (return a) or it isn't (return b)... there is no 3rd option that jumps over the if-statement and return a*b. This is exactly what happens to the line return guess; in your program.


You seem to be using some sort of bisection to find your value. But I don't understand your error term. normally in bisection you have a tolerance value epsilon and if (high - low) <= epsilon then you return a the current guess of (high + low)/2.

Here is the basic procedure for bisection:

inputs: low = lower bound, high = upper bound, epsilon - measure of minimum interval.
B1 guess = (high - low)/2
B2 if (high - low) <= epsilon then return guess as the approximation.
B3 if sign(f(high))*sign(f(guess)) <= 0 then low = guess else high = guess goto B1.

in this case f(x) = x*x - value.

Another note... your interval for number between 0 and 1 is wrong... note the at sqrt of .1 is 0.31622 which is not in the interval (0, .1). I would just make this interval (0, 1) for all values that that range.
User is offlineProfile CardPM
+Quote Post

soccerfreak
RE: Help With Recursive Square Root Function? (C++)
24 Nov, 2007 - 10:37 PM
Post #3

New D.I.C Head
*

Joined: 23 Nov, 2007
Posts: 2


My Contributions
thanks for the info, it was very helpful, and i was able to solve my errors and get my program working
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month