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

calculate the square root of a number using recursion

Page 1 of 1

2 Replies - 10088 Views - Last Post: 24 November 2007 - 11:37 PM Rate Topic: -----

#1 soccerfreak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 07

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

Posted 23 November 2007 - 11:54 PM

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?


# 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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: help with Recursive Square Root function? (C++)

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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

Posted 24 November 2007 - 11:48 AM

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:
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.
Was This Post Helpful? 0
  • +
  • -

#3 soccerfreak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 07

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

Posted 24 November 2007 - 11:37 PM

thanks for the info, it was very helpful, and i was able to solve my errors and get my program working
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1