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

Computing the Square Root of a Positive Number ---------------------------------

ID: 3621196 • Letter: C

Question

Computing the Square Root of a Positive Number

--------------------------------------------------------------------------------

Problem Statement
The square root of a positive number b can be computed with Newton's formula:

new x=1/2(x+b/x)

where x above starts with a "reasonable" guess. In fact, you can always start with b or some other value, say 1.

With b and a guess value x, a new guess value is computed with the above formula. This process continues until the new guess value and the current guess value are very close. In this case, either one can be considered as an approximation of the square root of b.

Write a function that takes in a double value and a tolerance, and computes the square root until the absolute error of two adjacent guess values is less than the tolerance value. The final estimate will be returned.

Write a driver program to input a number and a tolerance then output the square root found by your function.


--------------------------------------------------------------------------------

Program Input and Output
If the input are 10.0 for b and 0.00001 for the tolerance, an approximation of square root of 10 is found.
The estimated square root is 3.1622777

If the input are 0.5 for b and 0.00001 for the tolerance, an approximation of the square root of 0.5 is found.

The estimated square root is 0.707106769

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
double sqroot(double,double);

int main()
{double a,tolerence;
cout<<"Enter a positive number: ";
cin>>a;
cout<<"Enter a tolerence: ";
cin>>tolerence;
cout<<"If the input are "<<a<<" for b and "<<tolerence<<" for the tolerance,";
cout<<" an approximation of square root of "<<a<<" is found. ";
cout<<"The estimated square root is "<<sqroot(a,tolerence)<<endl;
system("pause");
return 0;
}

double sqroot(double a,double tolerence)
{double root,lastroot=1;

do
{
    root = 0.5 * (lastroot + a/lastroot);
    lastroot=root;
    } while((root*root - a)>= tolerence);

return root;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote