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

1) Create a program to repeatedly calculate in a function (call it mysqrt() ) th

ID: 3529277 • Letter: 1

Question

1) Create a program to repeatedly calculate in a function (call it mysqrt() ) the square root of a real number and then print the calculated value and the value found using the built-in square root function ( fabs() for real numbers; found in the cmath library). 2) You will need to declare your function in a prototype at the top of your main() function. 3) In main(), you will repeatedly loop asking for a value to find the square root of, then, calling both your square root function and the built-in square root function, you will present both values for comparison. Then, you will ask if the user wants to continue; if so, do it all again. 4) In mysqrt(), you will use Newton's Method to find the square root

Explanation / Answer

#include<iostream>

#include<cmath>

using namespace std;



//2) You will need to declare your function in a prototype at the top of your main() function.

double mysqrt(double);


/*

3) In main(), you will repeatedly loop asking for a value to find the square root of, then,

calling both your square root function and the built-in square root function, you will present both values

for comparison. Then, you will ask if the user wants to continue; if so, do it all again. */



int main(){

double n;

double again;


do{

cout<<"Enter value to find square root of: ";

cin>>n;

cout<<"The square root is "<<mysqrt(n)<<endl;

cout<<"Press number to continue or 0 to quit: ";

cin>>again;


}while(again!=0);


return 0;

}

/*

1) Create a program to repeatedly calculate in a function (call it mysqrt() )

the square root of a real number and

then print the calculated value and the value found using the built-in square root function

( fabs() for real numbers; found in the cmath library).


4) In mysqrt(), you will use Newton's Method to find the square root

*/

double mysqrt(double n){

n=fabs(n);

double fn=0.0001;

double fn1=0.5*(fn+(n/fn));

while((abs((fn1-fn)/fn1)*100.0)>=1){

fn=fn1;

fn1=0.5*(fn+(n/fn));

}


return fn1;

}