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

Programming language is C++. Show code and an example of the output on PuTTY whe

ID: 3788181 • Letter: P

Question

Programming language is C++. Show 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.

2. (20 pointsO Write your own cube root function named double my cbrt i(double n using the following Padé, approximant: 101,639 n2+207.953 n+29.7541. -n3+42.945 n2+215,165 n+gg, 1357 and then write a main which prints n cbrton), and my cbrt l(n) for n Tt times 10 to the kth power for k 100, -10, -1, 0, 1, 10, and 100. Use this code for (auto k 100, 10, -1, 0,1, 10, 100) n M PI pow(10.0, k); //cout goes here Name your program hw2pr2.cpp. Note: The formula above is an approximation and so for some values of n it will not be very accurate! Note: On Visual Studio or Xcode you may need to add a line like #define USE MATH DEFINES at the top of your code to activate M PI.

Explanation / Answer

I have written this code and executed in Ubuntu. I'm having updated version of gcc, which can compile c++17 also. The following is the code and output.

#include<iostream>
#include<cmath>
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;
}

Output:

$ g++ -std=c++17 -Wall -Wextra -pedantic -fsanitize=address,undefined hw2pr2.cpp
$ ./a.out
Pi value in my system:3.14159
By library function cube root of 3.14159e+100 is:3.15537e+33
By Pade approximant cube root of 3.14159e+100 is:-3.23527e-99
====================================================
By library function cube root of 3.14159e-10 is:0.000679803
By Pade approximant cube root of 3.14159e-10 is:0.322938
====================================================
By library function cube root of 0.314159 is:0.679803
By Pade approximant cube root of 0.314159 is:0.641188
====================================================
By library function cube root of 3.14159 is:1.46459
By Pade approximant cube root of 3.14159 is:1.45244
====================================================
By library function cube root of 31.4159 is:3.15537
By Pade approximant cube root of 31.4159 is:5.86251
====================================================
By library function cube root of 3.14159e+10 is:3155.37
By Pade approximant cube root of 3.14159e+10 is:-3.23527e-09
====================================================
By library function cube root of 3.14159e+100 is:3.15537e+33
By Pade approximant cube root of 3.14159e+100 is:-3.23527e-99
====================================================