Write a program to compute the square root of a number. DO NOT USE any math libraries in this program. You will be using the Babylonian method (a.k.a. Heron’s method) to approximate the square root. More information is available on wikipedia:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
To calculate the square root of x, the Babylonian method requires three inputs: x, an initial guess for the square root, and the error (tolerance). It uses a repetitive calculation to get closer and closer to the actual value of the square root:
nextGuess = lastGuess + xlastGuess2
After each repetition, the method checks if the absolute value of the difference between nextGuess and lastGuess is less than the error. If so, it stops and returns the value of nextGuess as the square root. If the difference between nextGuess and lastGuess is larger than the error, it repeats the calculation. Here is an example:
- Assume x is 16, the initial guess is 6, and the error is 0.5.
- So, the first repetition computes nextGuess to be (6 + 16 / 6) / 2 = 4.3333.
- Next, check the difference between the lastGuess (6) and nextGuess (4.3333). The absolute value of the difference is 1.6667, which is greater than the error of 0.5. So, repeat the calculation.
- The second repetition computes nextGuess to be (4.3333 + 16 / 4.3333) / 2 = 4.0128.
- Check the difference between lastGuess (4.3333) and nextGuess (4.0128). The absolute value of the difference is 0.3205, which is less than the error of 0.5. So, the procedure stops and returns 4.0128 as the square root of 16.
Your program needs three functions: main, square root, and absolute value. The main function should get all three inputs from the user (x, initial guess, and error), run the square root function, and output the approximate value of the square root. As always, you must validate the user's inputs. All three must be positive. If the user enters a value that is not positive, then your main function should prompt them to enter the value again (and repeat until they enter a valid input).
In addition, the square root function should keep track of how many repetitions are used to calculate the square root value, which should be printed out along with the square root value in the main function. As you can not use any math libraries, you will also need to write your own absolute value function. Only the main function should interact with the user (getting inputs and displaying results). The square root and absolute value functions must not contain any cin or cout statements.
Here is what I have:
#include <iostream>
using namespace std;
double square_root(double x, double lastGuess, double error);
double absolute(double difference);
double x, lastGuess, error;
double nextGuess;
double difference;
int count = 0;
int main()
{
cout << "Enter a value to take the square root of: ";
cin >> x;
cout << "Enter an ititial guess for the square root: ";
cin >> lastGuess;
cout << "Enter an error (tolerance) for the calculation: ";
cin >> error;
square_root(x, lastGuess, error);
cout << "After " << count << " iterations, the square root is approximately: " << nextGuess << endl;
return 0;
}
double square_root(double x, double lastGuess, double error)
{
do
{
nextGuess = (lastGuess + x / lastGuess) / 2;
count++; // this is the keep track of how many times i did the calculation until i get the closest sqrt value for x
if (absolute(nextGuess - lastGuess) < error)
{
break;
}
} while (difference > error);
return 0;
}
double absolute(double difference)
{
if (difference < error)
{
return nextGuess; // if its true it returns the sqrt value of the number the user entered for x
}
else
{
// if false it should continue to with the repetition but i don't know how to do that
// i need some help
}
}