Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When I run my code, I input the tolerance and then it doesn't return anything. Please tell me what in my code is causing this.

image text in transcribedimage text in transcribedimage text in transcribed

When I run my code, I input the tolerance and then it doesn't return anything. Please tell me what in my code is causing this. This is my code:

#include

#include

#include

using namespace std;

double arctanApprox (double x, double tol)

{

int i = 1;

double d= 1, sum = 0, prevsum = 0;

while (abs(prevsum-sum)

{

if (i%2==1)

{

sum = sum + (pow(x,d)/d);

}

else

{

sum = sum -(pow(x,d)/d);

}

i++;

d = d + 2;

}

return sum;

}

int main ()

{

char c;

double x, tol;

cout

cin >> tol;

while (tol

{

cout

cout

cin >> tol;

}

double result= 16.0 * arctanApprox ( 1.0/5.0, tol)- 4.0 * arctanApprox (1.0/239.0, tol);

cout

cout

cin >> c;

while (c == 'y');

{char c;

double x, tol;

cout

cin >> tol;

while (tol

{

cout

cout

cin >> tol;

}

double sum= 16.0 * arctanApprox ( 1.0/5.0, tol)- 4.0 * arctanApprox (1.0/239.0, tol);

cout

cout

cin >> c;

return 0;

}

}

(This is C++)

Probiem A: T Day IVMarch 14) IS Coming 20 points) constants such as and e play an important role in science and engineering. Nowadays, we can find (approximate) values of important constants such as by using a calculator or a math library. But how did people calculate these constants prior to electronic calcu- lators and computers? This problem asks you to implement one formula for calculating an approximation to Specifically, you will need to use the formula T 16 arctan (1/5) - 4 arctan (1/239) along with the arctangent series 7 arctan x- r This problem asks you to approximate T using these formulas. To begin, first write a function double arctanApprox(double x, double tol) that uses the arctangent series above to calculate an estimate for arctan(x). This function should first estimate arctan(x) using just one term from the series, then using two terms. If these two estimates are strictly within the tolerance, then return the two-term estimate. If not, find an estimate using three terms, and if that is strictly within the tolerance of the two-term estimate, return the three- term estimate. Otherwise continue in this fashion, generating a new estimate containing one more term in the series than the previous estimate, stopping when the new and previous estimates are strictly within the tolerance, and then returning the new estimate. Once you have your arctangent function working, notice that you can get an approximation for by using the arctangent series to give approximations for both 1.0/5.0 and 1.0/239.0 Once you have your arctangent function working, notice that you can get an approximation for T by using the arctangent series to give approximations for both 1.0/5.0 and 1.0/239.0, and then combining them as in the formula above. So write a main program that does the following: 1. Your program should first ask the user to input a tolerance. Validate that the input value is positive, if not, ask the user to input another value. Do this repeatedly until the user inputs a valid value 2. Your program should then approximate using the two equations above and your arctangent function. Specifically, it should call the arctangent function once for each t term in the equation, and with tolerance each time being one-twentieth of the user-input tolerance. So notice that the user-input tolerance and the tolerance value sent to the arctangent function are slightly different values 3. Your program should then output the approximation with 13 digits to the right of 4. The program should then ask if the user wishes to obtain another estimate. If the user answers 'y' or 'Y' then then the program should repeat the steps here. Otherwise it should terminate. Note that in doing this problem you should not use the arctangent functions from the cmath library; rather you must use the series above to approximate the arctangent. Here are a couple examples (Formatting note, only one space is used or expected in all places in this example) Example T: Input tolerance .1 Pi approximation 3.1405970293261 Do you wish to approximate pi again? y Input tolerance: .001 Pi approximation 3.1415917721832 Do you wish to approximate pi again? n Example 2: Input tolerance 42 Tolerance must be positive. Input tolerance 4.2 Tolerance must be positive. Input tolerance 00042 Pi approximation 3.1415917721832 Do you wish to approximate pi again? Y Input tolerance 1E-8 Pi approximation 3.1415926536236 Do you wish to approximate pi again? n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions