Write your own squareroot function named double my_ squareroot _1 (double n) usi
ID: 3847731 • Letter: W
Question
Write your own squareroot function named double my_ squareroot _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, squareroot (n), and my_ squareroot _1(n) for n = 3.14159 times 10 to the k^th power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which may not work on older versions of Visual Studio): for (auto k: {-100, -10, -1, 0, 1, 10, 100}){ n = 3.14159 *pow(10.0, k);//cout goes here } Modify problem 1 to also print the relative error as a per cent, by adding a column relative_error_per_cent = 100*((my_ squareroot _1(n)- squareroot (n)/ squareroot (n). Line up the columns using setw0, etc. Name your program hw2pr2.cpp.Explanation / Answer
square root function: double my_sqrt_1 (double n) { double x = 1; // use for loop to repeat 10 times as it is more convenient 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 ) ; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.