HElP!
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 = 3.14159 times 10 to the kth power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which only works on linux2):
for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
n = 3.14159 * pow(10.0, k);
//cout goes here
}
Note: my_sqrt_1(n) is based on the Newton-Raphson algorithm
and this is what i have so far but im so lost as i am a complete beginner at programming nevertheless at c++.
#include "std_lib_facilities_3.h"
double my_sqrt_1 (double n)
{
int x=1;
x = (x+n/x)/2;
return x;
}
int main ()
{
for (auto k: {-100, -10, -1, 0, 1, 10, 100}){
n= 3.14159*pow(10.0, k);
cout << n << " , " << sqrt(n) << " , " << my_sqrt_1(n)\n;
}
}
Within the function my_sqrt_1(), you have omitted to implement this part of the specification: "repeat 10 times". Variable x needs to be of type double, not int.
In main(), variable n is used without being defined and given a type.