In: Computer Science
IN C++
Using newton's method, Please create a SqRoot function.
DO NOT USE sqrt ( ) , If you do I will give you zero points period.
So create a function that takes a double and returns a double that is the square root of the number.
You must decide what number to start the calculations with,
You must decide how and when to stop computing.
For this exercise you must tell me ( use comments in your code: /* */, etc. )
There is not 'correct' answer but if you do not write and address these questions you will get a very low score.
A) how you chose the initial estimate and why , or if it is important
B) How you decided on how to end the cycles of calculation and why
C) Tell me about some of your test cases and what you learned from them.
Here is the formula is psudo C++ Code:
// A = approximation of the Square Root
// Num = number that we want to find the square root of.
// A_prime is the updated value of a after the formula
A_prime = ( ( N / A ) + A ) / 2.0;
#include <iostream>
using namespace std;
// function declaration
// takes a double as parameter and
// return a double as square root of the given number
double SqRoot(double num);
int main() {
cout << "Square root of 4.0 is: " <<
SqRoot(4.0) << endl;
cout << "Square root of 1.0 is: " <<
SqRoot(1.0) << endl;
cout << "Square root of 9.0 is: " <<
SqRoot(9.0) << endl;
cout << "Square root of 25.25 is: " <<
SqRoot(25.25) << endl;
cout << "Square root of 100.0 is: " <<
SqRoot(100.0) << endl;
cout << "Square root of 8.0 is: " <<
SqRoot(8.0) << endl;
cout << "Square root of 3600.0 is: " <<
SqRoot(3600.0) << endl;
cout << "Square root of 0.5 is: " <<
SqRoot(0.5) << endl;
cout << "Square root of 0.0 is: " <<
SqRoot(0.0) << endl;
return 0;
}
double SqRoot(double num) {
/*
* chose the initial estimate of Square root as 1
* becouse square root of 1 is 1. its an
identity.
* in this method of finding square root we are
adding
* up the estimation and dividing it by 2 so chossing
minimum
* posible value as initial is good choice
*/
// take approximation of the Square Root
double approx = 1.0;
// use the formula
// A_prime = ( ( N / A ) + A ) / 2.0;
// A_prime is the updated value of a after the
formula
// we will use the formula for calculation till we
find the square root
/*
* we end the cycles of calculation when the square of
aproximation value is equal
* to the given number,thus by defination aproximation
value is the
* square root of the given number
* in the loop we update approximate value based on
return value of formula.
* using this method it is easy to find square root of
perfect square number
* but its hard to find square root of imperfect number
as error in floating point value
* repesented by computer(overflow). because of this in
accuracy we stop loop as we find nearest
* value to the square root by 0.01% accuracy.
* this allow us to get any positive number square root
by 0.01% accuracy
*/
while (approx*approx <= num*0.9999 || approx*approx
>= num*1.0001) {
// calculate A_prime
approx = ((num / approx) + approx)
/ 2.0;
}
/*
* using negative number as test case result in
infinite loop
* its not possible to find square root of neagtive
number by this method.
* its not possible to find square root of zero as its
returns not a number
* as result of division by 0 when aprox reaches its
value to 0 in the loop
*/
return approx;
}
i have added comments in the code. let me know if you have any doubts or problem running the program or want any modification in program.