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

New Topic/Question
Reply




MultiQuote




|