Question
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:
Each term in the series has this common pattern:
where m is 1, 3, 5, 7,...... and n is 0, 1, 2, 3, 4.......
The first term in the series has the value
where m is 1 and n is 0.
The second term has the following value
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:
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started