While loop Write a C++ program using a while loop for Programming Exercise 5.27
ID: 3670272 • Letter: W
Question
While loop Write a C++ program using a while loop for Programming Exercise 5.27 on p 198 with the following change: Instead of displaying it fix values of i shown, use a while loop to continue adding terms until n is accurate to 6 digits after the decimal point. Pi=4(1-1/3+1/5-1/7+1/9-1/11+....) Display the following and turn in printouts of the program and the results. The number of terms needed to find pi The value of pi using aces(-1) using 10 digits after the decimal point The value of pi/2 found with the series using 10 digits after the decimal point (the first 6 digits after the decimal point should match the value above).Explanation / Answer
Basically the above is a series expansion of pi for approximating pi.
In math's terms, it is
. . . . . N
. . . . ----- . . (-1)^i
= 4 . . . -----------
.. . . . /__ . . 2i + 1
. . . . i = 0
=> = 4[1 -1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ... - 1/(2N - 1) + 1/(2N + 1)] for some large positive integer N.
As you can see, each term a_i (supposed to be a subscript i)in the sequence is 1/(2i + 1) and is added or subtracted alternately.
If we assume N = 10000, the above would be transformed into the following C++ code:
double pi = 0.0, sign = 1.0;
for (double i = 0.0; i < 10000.0; i += 1.0) {
pi += sign / (2 * i + 1.0);
sign *= -1.0;
}
pi *= 4.0;
Now, that's pretty inefficient, calculating the sign for each term, so we start calculating 2 terms at a time since we already know they alternate between positive and negative.
Mathematically, that's:
. . . . . N
. . . . ----- . . . 1 . . . . . . . 1
= 4 . . .--------- . - . -----------
.. . . . /__ . 4i + 1 . . . . 4i + 3
. . . . i = 1
=> = 4[(1 -1/3) + (1/5 - 1/7) + (1/9 - 1/11) + ... + (1/(4N + 1) - 1/(4N + 3))] for some large positive integer N.
As C++:
double pi = 0.0;
for (double i = 1.0; i < 10000.0; i += 1.0) {
pi += 1.0 / (4 * i - 1.0) - 1.0 / (4 * i + 1.0);
}
pi *= 4.0;
Now, if we increase the iterator by 4 instead of 1, we can get rid of the 4 * i in the loop.
As Maths:
. . . . ----- . . 1 . . . . . . 1
= 4 . . ------- . - . --------- . for i = 1, 5, 9, ..., 4N + 1
.. . . . /__ . . i . . . . . i + 2
. . . . . . i
Now as C++. Note that the condition on the loop changed from the previous to maintain the same number of calculations as the Maths version if we take N as being 10000:
double pi = 0.0;
for (double i = 1.0; i < 40000.0; i += 4.0) {
pi += 1.0 / i - 1.0 / (i + 2.0);
}
pi *= 4.0;
We can now simplify the expression so there are less multiply and divide operations.
As Maths:
. . . . ----- . . . 2
= 4 . . ------------ . for i = 1, 5, 9, ..., 4N + 1
.. . . . /__ . i(i + 2)
. . . . . . i
As C++:
double pi = 0.0;
for (double i = 1.0; i < 40000.0; i += 4.0) {
pi += 2.0 / (i * (i + 2.0));
}
pi *= 4.0;
--------------------------------------...
Now, for the 10000, 20000, and (what the last number probably should be) 100000 values, they just state how many iterations it has to go through, with more iterations meaning a more accurate value for .
Using different values for N, I got
. . . . N . . . . | . . . .
-------------------|------------------...
. . . . 10 . . . .| . 3.096161526
. . . 100 . . . .| . 3.136642189
. . . 1000. . .| . 3.141093153
. . 10000. . .| . 3.141542659
void pi(int n, double *pi_value, int *iterations)
{
int i;
int ADD = 1; // 1 = true & 2 = false
for(i=0; i<n; i++)
{
switch(ADD)
{
case 1:
*pi_value += (4/((n*2)-1));
ADD = 2;
break;
case 2:
*pi_value -= (4/((n*2)-1));
ADD = 1;
{ break;
}
(*iterations)++;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.