Write your own squareroot function named double my_ squareroot _1(double n) usin
ID: 3847884 • 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 tunes 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). Line up the columns using setw(), ect. Name your program hw2pr2 cpp. Modify problem 2 to scale n so the squareroot will possibly be more accurate, as follows: Name a function my squareroot _2 (n), which does this: Set result to 1. If n is greater than 8/5, divide n by - 4 and multiply result by 2; repeat until n is not greater than 8/5. If n is less than 2/5, multiply n by 4 and divide result by 2, repeat until n is not less than 2/5. Return result times my_ squareroot _1 (n) The main should print n, squareroot (n), my_ squareroot _2(n). and the relative error of my_ squareroot _2 (n) as a per cent. Name your program hw2pr4 cpp.Explanation / Answer
Hi, Since you have asked only 4, i am assuming you already have 1 and 2 i.e.my_sqrt_1(n) so will go ahead and use it.
4. double my_sqrt_2(double n)
{
double result=1;
if(n>8/5)
{
while(n>8/5) // repeat until n is greater than 8/5
{
result=result*2;
n=n/4;
}
}
else if(n<2/5)
{
while(n<2/5)// repeat until n is less than 2/5
{
result=result/2;
n=n*4;
}
}
return result*my_sqrt_1(n);
}
Thumbs up if this was helpful, otherwise let me know in comments.
GoodDay.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.