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

Programming language is C++. The code for problem 2 is posted below. Show finish

ID: 3788917 • Letter: P

Question

Programming language is C++. The code for problem 2 is posted below. Show finished code and an example of the output on PuTTY when done. Use g++ -std=c++17 -Wall -Wextra -pedantic -fsanitize=address,undefined to compile the code on PuTTY.

#include<iostream>
#include<cmath>
#include <stdlib.h>

using namespace std;

/*
Calculate cube root using Pade approximation
*/
double my_cbrt_l(double n)
{


   double numerator = (101.639 * n * n) + (207.953*n) + 29.7541;
   double denominator = (-1 * n * n *n) + (42.945* n *n) + (215.165* n) + 92.1357;

   double result = numerator / denominator;

   return result;
}

int main(void)
{

   double n;

   cout << "Pi value in my system:" << M_PI << endl;

   for (auto k : { -100,-10,-1,0,1,10,100 }) {
       n = M_PI * pow(10.0, k);
       cout << "By library function cube root of " << n << " is:" << cbrt(n) << endl;
       cout << "By Pade approximant cube root of " << n << " is:" << my_cbrt_l(n) << endl;
       cout << "====================================================" << endl;
   }
   return 0;
   try {
       return 0;
   }
   catch (exception&e) {
       cerr << "error:" << e.what() << ' ';
       keep_window_open();
       return 1;
   }
   catch (...) {
       cerr << "Oops:unknown exception! ";
       keep_window_open();
       return 2;
   }
}

3. (20 points Modify problem 2 to also print the relative error as a per cent, by adding a column. relative error per cent 100 my cbrt i(n)-cbrt(n) cbrt(n). Line up the columns using setw0, etc. Name your program hw2pr3.cpp.

Explanation / Answer

//main.cpp


#include "std_lib_facilities_4.h"

double my_cbrt_1(double n){
   if(!n) error("Wrong data type, please enter a double");
   return (-0.411665*n*n)+(1.03455*n)+(0.377113);
}

double my_cbrt_2(double n){
   double result {1};
   if(!n) error("Wrong data type, please enter a double");
       while(n>1){
           result*=2;
           n/=8;
       }
       while(n<1/8){
           result/=2;
           n*=8;
       }
   return result*my_cbrt_1(n);
}

int main(){

   try{
       cout << setw(20) << "n" << setw(20);
       cout << "cbrt(n)" << setw(20);
       cout << "my_cbrt_2(n)" << setw(20);
       cout << "relative error" << setw(20) << ' ';
       for(auto k: {-100,-10,-1,0,1,10,100}){
           double n = M_PI * pow(10.0,k);
           double relative_error_per_cent = 100*(abs(my_cbrt_2(n)-cbrt(n))/cbrt(n));
           cout << setw(20) << n;
           cout << setw(20) << cbrt(n);
           cout << setw(20) << my_cbrt_2(n);
           cout << setw(20) << relative_error_per_cent << " %" << ' ';
       }
   }
   catch(runtime_error& e){
       cerr << e.what() << ' ';
   }
   catch(...){
       cerr << "Oops: some exception! ";
   }
}