There are several techniques for implementing the sqrt function as found in the
ID: 3551632 • Letter: T
Question
There are several techniques for implementing the sqrt function as found in the cmath
header. One such technique is known as the Babylonian method.
1 It approximates the square
root of a number, num, by repeatedly performing a calculation using the following formula:
nextGuess = ( lastGuess + num / lastGuess ) / 2;
When nextGuess and lastGuess are almost identical, nextGuess is the approximated
square root. The initial guess can be any positive value (e.g., 1). This value will be the
starting value for lastGuess. If the difference between nextGuess and lastGuess is
less than a very small number, such as 0.000001, you can claim that nextGuess is the
approximated square root of num. If not, nextGuess becomes lastGuess and the
approximation process continues.
Implement functions from the following prototypes:
// returns the square root of num
double squareRoot( double num );
// returns the absolute value of num
void absoluteValue( double& num );
// neatly prints a table of values and their square roots
void printTable( ofstream& ofs, int begin, int end, int step );
Write a program (pa05.cpp) that prints the following table formatted exactly as shown
below. Your output should be saved to a file named output.txt:
Number SquareRoot
------ ----------
0 0.0000
2 1.4142
...
48 6.9282
50 7.0711