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

Write this program in C. The square root of a number N can be approximated by re

ID: 3683010 • Letter: W

Question

Write this program in C.

The square root of a number N can be approximated by repeated calculation
using the formula
NG = 0.5(LG + N/LG)
where NG stands for next guess and LG stands for last guess. Write a function
that calculates the square root of a number using this method.
The initial guess will be the starting value of LG . The program will compute
a value for NG using the formula given. The difference between NG
and LG is checked to see whether these two guesses are almost identical. If
they are, NG is accepted as the square root; otherwise, the next guess ( NG )
becomes the last guess ( LG ) and the process is repeated (another value is
computed for NG, the difference is checked, and so on). The loop should be
repeated until the difference is less than 0.005. Use an initial guess of 1.0.
Write a driver function and test your square root function for the numbers
4, 120.5, 88, 36.01, 10,000, and 0.25.

Modify the program according to these parameters:

- Include a do…while loop to check that the input number n > 0.
- Use constant macros for the initial guess (1.0) and accuracy. Change the accuracy
from 0.005 to 0.000002.
- You will write your own square root function, called mysqrt(). Inside the
function, use a do while loop. For each iteration in the loop, you will
calculate a new guess from the last guess until the new guess and last guess are
approximately equal.
- Create a counting variable count that keeps track of how many times you go
through the do...while loop in the function mysqrt().
- Below is a sample run. Complete the sample run with your calculator:

Explanation / Answer

//****************************************************************
//
// Program to compute square roots by iteration
//
//****************************************************************
//
// Main Program
// loop until a sentinel is found,
// reading n, a guess for sqrt(n), and an epsilon error bound
// call the myroot function to compute a square root of n
// print the result
// check the accuracy of the square of the square root vs. n
//
// Function myroot
// iterates nextguess = 0.5 * (lastguess + n/lastguess)
// until abs(lastguess-nextguess) is within epsilon
//
//****************************************************************
//
// main program variables used (all doubles):
// epsilon -- the tolerance (error bound) on the square root
// firstguess -- an initial guess for the square root
// theroot -- the root found by our function
// value -- the value n of which to take the square root
//
// defined constants:
// SENTINEL set to -9999.0
//
//****************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define YES 1
#define NO 0

#define SENTINEL -9999.0

double myroot(double val, double firstguess, double eps);

//************************************************************************
//
//************************************************************************
int main(int argc,char *argv[])
{
double epsilon,firstguess,theroot,value;
do
{
scanf("%lf %lf %lf",&value,&firstguess,&epsilon);

while((SENTINEL != value) && (SENTINEL != epsilon) && (SENTINEL != firstguess))
{
printf("echo value,firstguess,epsilon: %lf %lf %lf ",
value,firstguess,epsilon);

theroot = myroot(value,firstguess,epsilon);

printf("root of %lf is %lf ",value,theroot);
printf("absolute error when squaring back is %le ",
fabs(value-theroot*theroot));
printf(" ");
scanf("%lf %lf %lf",&value,&firstguess,&epsilon);
}
}while(value>0) // end while

return(1);

} /* end main */

//************************************************************************
//
// function myroot
// input arguments (all doubles):
// val -- the value of which to take the square root
// firstguess -- the initial guess for the square root
// eps -- the error bound for when to quit iterating
//
// other variables:
// long count -- the iteration counter for reference purposes
// long done -- the done flag for when to quit iterating
// lastguess -- the previous guess in the iteration
// nextguess -- the computed next guess in the iteration
// theroot -- the final square root returned to the calling program
//
// function process
// we loop forever until done
// compute nextguess = 0.5 * (lastguess + val/lastguess)
// bump a counter and print intermediate results
// if the absolute value (lastguess-nextguess) is < eps, we're done
// when done, assign nextguess to theroot and return
//
//************************************************************************
double myroot(double val, double firstguess, double eps)
{
long count,done;
double lastguess,nextguess,theroot;

printf("enter myroot: val,firstguess,eps: %lf %lf %lf ",val,firstguess,eps);

count = 0;
done = NO;
lastguess = firstguess;
while(!done)
{
nextguess = 0.5 * (lastguess + val/lastguess);

count++;
printf("count,prev,next: %ld %lf %lf ",count,lastguess,nextguess);
if(fabs(lastguess - nextguess) < eps)
done = YES;

lastguess = nextguess;
}

theroot = nextguess;

printf("leave myroot: theroot: %lf ",theroot);
return(theroot);
} // end of double myroot(double val, double firstguess, double eps)

input:

out put: