Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topic: Use of any loop statement (chapter 5). There is NO need to use any if/else statement. Create a new C++ project titled HW 5

Topic: Use of any loop statement (chapter 5). There is NO need to use any "if/else" statement.

Create a new C++ project titled HW 5 in the CodeBlocks IDE. Use the Console Application project option.

Use loop to approximate the PI value

In the 14th century, Babylonian mathematicians approximated the value of PI manually by calculating this series:

image text in transcribed

Each term in the series has this common pattern:

image text in transcribed

where m is 1, 3, 5, 7,...... and n is 0, 1, 2, 3, 4.......

The first term in the series has the value

image text in transcribed

where m is 1 and n is 0.

The second term has the following value

image text in transcribed

where m is 3 and n is 1

Notice that the signs of each term alternate between + and -.

The program should ask users to enter the number of terms (termCount) for the series. The program then iterates termCount times to estimate the PI value and display it. The larger the input termCount is, the more accurate the value of estimated PI is.

You should use an input validation loop to ensure that the user enters a value of termCount that is greater than 0. If a user enters a count that is less than 0, keep asking for another input.

The program should display the estimated PI value with 15 decimal digits in the fractions. You should use the square root function and power function in the "cmath" library.

You should also display the value of the M_PI constant in the C++ cmath library with 15 decimal fraction digits after decimal point. Displaying both the calculated value and the C++ M_PI constant provides an easy way to compare and see the difference.

Here are sample outputs:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Hints:

1-You can use any loop type. Do not use nested loop because it is wasteful and it makes your code more complex.

2-Inside the loop, to alternate the sign of each term between 1 and -1, it is NOT efficient to use an "if" statement to test for evenness or oddness of term count. Instead, it is better to use an integer variable for the multiplication factor that should be initialized to 1, and then put minus sign in front of variable in the loop to flip the sign like this:

factor = -factor;

3-Use setw() to align the numbers in the output nicely. Use fixed and setprecision() to control the number of digits.

4-Try to avoid integer division because it will give incorrect values. If your output is 3.4.. at the end of 4 iterations, it is because of the integer division issue.

5-Calculation of the square root of 12 should be done only ONCE outside the loop and store in a constant (before loop) because invoking the sqrt () function multiple times is wasteful of computer time.

6-There is no reason to use float variables because they have less precision than double variables.

7-If the M_PI constant gives you error with CodeBlocks, you need to put these lines at the top of your code in the correct order before cmath:

#define _USE_MATH_DEFINES

#undef __STRICT_ANSI__

#include

:(- *8 + 9 ..) V12 x 1/(m x3") V12 -1 V12 x - 3x3 This program estimates the value of PI using the Babylonian method. Enter a count that will be used for series iteration: 1 1 3.464101615137754 Estimated PI with 15 decimal fraction digits is 3.464101615137754 PI value in the cmath library is 3.1415 9265 3589793 Process returned o (0x0) Press any key to continue. execution time : 2.595 s This program estimates the value of PI using the Babylonian method. Enter a count that will be used for series iteration: 10 1 3.464101615137754 3.079201435 678004 3.15 6181471569954 4 3.13785 2891595 680 3.142604745663084 6 3.141308785462883 7 3.141674312698837 8 3.141568715941784 9 3.141599773811505 10 3.1415 90510938080 Estimated PI with 15 decimal fraction digits is 3.1415 90510938080 PI value in the cmath library is 3.141592653589793 execution time : 11.348 S Process returned o Ox0)_ Press any key to continue. This program estimates the value of PI using the Babylonian method. Enter a count that will be used for series iteration: 20 1 3.464101615137754 2 3.079201435 678004 3.156181471569954 4 3.1378528915 95 680 3.142604745663084 6 3.141308785462883 3.141674312698837 3.141568715 941784 3.141599773811505 3.1415 90510938080 11 3.1415 93304503081 12 3.141592454287646 133.141592715020380 14 3.1415 92634547314 15 3.14159265 9521713 16 3.1415 92651733997 17 3.1415 92654172575 18 3.1415 9265 3406165 19 3.1415 9265 3647826 20 3.1415 9265 35 71403 10 Estimated PI with 15 decimal fraction digits is 3.1415926535 71403 PI value in the cmath library is 3.14159265 35 89793 Process returned o (Ox0) Press any key to continue. execution time : 3.269 s This program estimates the value of PI using the Babylonian method. Enter a count that will be used for series iteration: 25 1 2 3 4 5 10 12 3.464101615137754 3.079201435 678004 3.15 6181471569954 3.1378528915 95 680 3.142604745663084 3.141308785462883 3.141674312698837 3.141568715941784 3.141599773811505 3.141590510938080 3.141593304503081 3.141592454287646 3.1415 92715020380 3.141592634547314 3.14159265 95 21713 3.141592651733997 3.141592654172575 3.14159265 3406165 3.14159265 3647826 3.1415 9265 35 71403 3.141592653595635 3.141592653587933 3.14159265 35 90386 3.1415 9265 35 89603 3.14159265 35 89854 14 16 18 20 23 24 Estimated PI with 15 decimal fraction digits is 3.141592653589854 PI value in the cmath library is 3.14159265 3589793 Process returned o (Oxo) Press any key to continue. execution time : 3.243 s This program estimates the value of PI using the Babylonian method. Enter a count that will be used for series iteration: 0 The input count must be greater than 0. Try entering again: 3 2 1 2 3.464101615137754 3.079201435 678004 3.156181471569954 3 Estimated PI with 15 decimal fraction digits is 3.156181471569954 PI value in the cmath library is 3.14159265 35 89793 Process returned o (Oxo) Press any key to continue. execution time : 11.856 S

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

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

Construct a function table for each given ALU circuits

Answered: 1 week ago