In: Computer Science
Write three functions that compute the square root of an argument x using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number x is the value s such that s*s == x. For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that x is in the range 0.0 to 100.0. You program should have a main() that asks the user for x and an accuracy bound, and then prints out the approximate square root for each of the three methods. If x is negative write an error message and exit. One. Write a pure function double sqrtLS(x, accuracy) that computes the square root of x using Linear Search. Do this in a loop that starts s at 0.0 and bestError at x. Each iteration of the loop computes error = |x-s*s|. If error< bestError, set bestError to error, and bestS to the current s. Then increment s by accuracy and move on. At the end of the loop, return bestS. This method is linear search, it searches along the number line for the best s that is within accuracy of the true value. Use the function double fabs(double) from the math library to compute absolute value. Two. Write a pure function double sqrtBS(x,accuracy) that computes the square root of x using Binary Search. (This method is also called bisection.) Start a variable low at 0.0 and a variable high at 10.0. This assumes that x will be no larger than 100.0 In a while loop do this: Compute the midpoint mid=(low+high)/2. If |x-mid*mid| ≤ accuracy return mid. If mid*mid < x set low = mid. Else set high = mid. Three. Write a pure function double sqrtNM(x, accuracy) that computes the square root of x using Newton’s Method. Recall the method: Start out an initial guess s=1.0 (or any value). Now repeatedly improve the guess: s = (s + x/s)/2 When the guess meets the ending condition |x-s*s| is ≤ bound return s. M:\ClassApps>gcc -lm squareRoot.c M:\ClassApps>.\a Enter x --> 47.5 Enter error bound --> 1e-5 sqrtLS(47.500)= 6.892020; s*s = 47.499940 sqrtBS(47.500)= 6.892024; s*s = 47.499999 sqrtNM(47.500)= 6.892024; s*s = 47.500001 Use ANSI-C syntax. Do not mix tabs and spaces. Do not use break or continue.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float sqrtLS(float x,float accuracy)
{
int arr[100],i=0;
float bestError=x,s=0,error,bestS;
for( s=0;s<100;s+=accuracy)
{
error=abs(x-s*s);
if(error<bestError)
{
bestError=error;
bestS=s;
}
}
return bestS;
}
float sqrtBS(float x,float accuracy)
{
float low=0,high=10.0;
float mid=(low+high)/2;
while(1)
{
mid=(low+high)/2;
if(abs(x-mid*mid)<=accuracy)
{
return mid;
}
else if(mid*mid<x)
{
low=mid;
}
else high=mid;
}
}
float sqrtNM(float x,float accuracy)
{
float s=1.0;
while(1)
{
s = (s + x/s)/2;
if((abs(x-s*s))<= accuracy)
{
return s;
}
}
}
int main() {
float x;
float accuracy,ans;
printf("Enter x");
scanf("%f",&x);
if(x<0)
{
printf("Invalid Input\n");
}
else
{
printf("Enter Accuracy\n");
scanf("%f",&accuracy);
ans= sqrtLS(x,accuracy);
printf("Square Root by Linear Search %f\n",ans);
ans= sqrtBS(x,accuracy);
printf("Square Root by Binary Search %f\n",ans);
ans= sqrtNM(x,accuracy);
printf("Square Root by Newton Method %f\n",ans);
}
return 0;
}