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

Write a program named sqroot. cpp to compute tne squareroot s of an unlimited nu

ID: 3774244 • Letter: W

Question

Write a program named sqroot. cpp to compute tne squareroot s of an unlimited number of non-negative double values entered as command line arguments. This problem requires you to write one recursive function named newton, one auxiliary function named sqroot that calls newton properly, and a main function that will call sqroot. #include to access the constant FLT_EPSILON that should be used as a measure of successive improvement (the value of epsilon in the algorithm description below), and #include to use the fabs function (the absolute value function for floating point values). Write function newton to take two double values, x and a, and return the double value that best approximates the squareroot of x. Use Newton's Method, described once more by Prof. Standish: "If |a*a - x| lessthanorequalto epsilon, we stop with the result a. Otherwise we replace a with the next approximation, defined by (a + x/a)/2. Then, we test this next approximation to see if it is close enough and we stop if it is. In general, we keep on computing and testing successive approximations until we find one close enough to stop." Write function sqroot to take one double argument, x. This auxiliary function simply makes the initial call to newton with an initial approximation equal to x/2, and returns the result of that function call. main must extract as many numbers as the user types on the command line. For each valid entry, invoke the squareroot function and print results. Negative values are not valid. Match the output of our solution, including the handling of usage errors - note the program does not stop processing if it encounters a bad argument.

Explanation / Answer

#include<iostream>
#include<cmath>
#include<cfloat>
#include<string>
#include <cstdlib>
using namespace std;

//declare sqroot function calls recursive function newton
double sqroot1(double num );
double newton(double num, double guess);
int main(int argc, char **argv)
{
   if( argc < 2 )
   {
       cout<<"Enter the numbers as command line to find square roots of those numbers "<<endl;
       return -1;
   }

   for( int i = 0 ; i < argc ; i ++)
   {
       cout<<"Number = "<< argv[i+1] <<endl;
       cout<<"sqroot of number"<<i+1<<" : "<< sqroot1(atoi(argv[i+1])) <<endl;
   }
   //test without command args
   /*char s[]="9";
   cout<<"s= "<<atoi(s);
   cout<<"sqroot of number "<<" : "<< sqroot1(atoi(s)) <<endl;*/
}

double newton(double num, double a)
{
  

           if ((abs(a*a - num) <= FLT_EPSILON))
           {
               return a;
           }
           else
           {
               newton(num, (a+num/a)/2 );
           }
      
  
}
double sqroot1(double num)
{
   double sqrt = newton(num,num/2);
   return sqrt;

}

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