Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(20 points)Write your own square root function named double my_sqrt_1(double n)

ID: 3562917 • Letter: #

Question

(20 points)Write your own square root function named double my_sqrt_1(double n) using the following pseudocode:

x = 1

repeat 10 times: x = (x + n / x) / 2

return x

and then write a main which prints n, sqrt(n), and my_sqrt_1(n) for n =

? times 10 to the kth power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which may not work on Visual Studio):

            for(auto k : {-100, -10, -1, 0, 1, 10, 100}){

                        n = M_PI * pow(10.0, k);

                        //cout goes here

            }

Note: my_sqrt_1(n) is based on the Newton-Raphson algorithm.

Explanation / Answer

double my_sqrt_1 (double n) { double x = 1 ; // repeat 10 times for( int i = 0 ; i < 10 ; ++i ) x = (x+n/x)/2 ; return x; } int main () { for (auto k : { -100, -10, -1, 0, 1, 10, 100 } ) { double n = 3.14159 * pow( 10.0, k ) ; cout