We were assigned the following problem as a large project in my Freshman intro to C++ programming class:

We have to determine the number of terms needed to calculate Pi to a specific number (3.0, 3.1, 3.14, 3.141, 3.1415, and 3.14159), using the formula:

pi= 4(1-1/3+1/5-1/7+1/9-1/11+1/13...)

I thought I had the code figured out pretty easily, but I have having a problem. I was going to use the following (incomplete) code as a model:

``````// Date: 10/30/06
// Homework #07
// Student: Jim Hummel
// Course:  CSE 121
// This program calculates the number of terms required for an approximation of pi.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double pif(double q);
int terms=0;
int main()
{double q=3.1;
cout << "Pi Value    Terms\n--------------------------\n";
cout << fixed << setprecision(2);
cout << pif(q)<< "        "<< terms << endl;
return 0;
}
double pif (double q)
{double n=1.0;
double pi=0;
while (pi<(q-(1.0/q)) && pi>(q+(1.0/q)))
{
pi = pow(-1.0,n)/(2.0*n-1.0);
n++;
terms++;
}
pi*=4.0;
return pi;
}``````

In the main function there, I was going to try to have it call the function pif() once for each value q that I set to it, then cout the results, and the number of terms.

I've been toying around with possible options (as seen in the while statement there), but no matter what I do, it returns a value of o terms and 0.00 for pi each time.

What am I doing wrong?

Thanks in advance, this is killing me.

Sorry, q was to represent the question.

I was going to have it so that I would set q to 3.0 and send it to pif()... then cout the results, and then set q to 3.1... repeat with 3.14, 3.141, all the way to 3.14159, which is as far as I need to go. I need to know the number of terms used for each of those values (how many fractions were necessary to get pi to equal that number.)

Thanks.

Maybe this could help: This is the exact problem:

Problem 2. [The Number p] The number p may be calculated using the following infinite series:
p = 4(1-1/3+1/5-1/7+1/9-1/11+1/3
How many terms of this series you need to use before you get the approximation p of p, where:
a) p = 3.0
b) p = 3.1
c) p = 3.14
d) p = 3.141
e) p = 3.1415
f) p = 3.14159
Write a C++ program to answer this question.

I don't understand why you are testing how close the value calculated is to 1/q. But anyway, here is a more refined way of writing the source. The algorithm is the same, so any errors are due to that.
Do not use global variables unless absolutely necessary.

``````#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double pif(double q);
int terms=0;
int main()
{
double q=3.1;
cout << "Pi Value    Terms\n--------------------------\n";
cout.setf( ios::fixed );
cout.precision (2);
cout << pif(q)<< "        ";
cout << terms << endl;  // Since you are changing the value of terms in the above line,
// do not output it in the same line. The result is undefined.
// Use a seperate line to output terms.
// But a better way will be to output the value of n in pif()
return 0;
}
double pif (double approx) // Use meaningful parameter names rather than just p, q, r
{
long n=0;
double pi=0.0;
double error = fabs( pi - approx );
double tolerance =  1 / approx; // Assuming approx is positive, this is also +ve
//     while (pi<(q-(1.0/q)) && pi>(q+(1.0/q)))// Actually this should be
// while (pi<(q-(1.0/q)) || pi>(q+(1.0/q))).
// It is the same as | pi -q | > ( 1.0 / q )
// |x| = absolute value of x
while ( error > tolerance )
{
n++;
term++;
pi = pi + pow( -1, n + 1)/(2 * n - 1 );
error = fabs( pi - approx);
}
pi*=4.0;
// cout << pi << "        "<< n << "         " << error << endl;
return pi;
}``````

Thanks for the reply, although I didn't read it in time before I had to hand it in.

I worked on building an entirely new code without the rediculous while statements... it led to a very long, messy, and unoganized code, but it worked.

I spent the remainder of my time trying to turn it into a while loop, using another variable for the setprecision (as seen below) and the q-(number) statements, but this code here did work and got the job done:

``````// Date: 10/30/06
// Homework #07
// Student: Jim Hummel
// Course:  CSE 121
// This program calculates the number of terms required for an approximation of pi.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{double q=3.0;
double n=1;
bool check=false;
double pi = 0;
int terms=0;
while (!check)
{
pi += (double) pow(-1, n+1)/(2*n-1);
cout << fixed << setprecision(1);
if (q-.1<=pi*4 && q+.1>=pi*4)
check=true;
else
terms++;
n++;
}
pi *= 4;
cout << terms << "        " << pi << endl;

//Part 2//

q=3.1;
n=1;
check=false;
pi = 0;
terms=0;
while (!check)
{
pi += (double) pow(-1, n+1)/(2*n-1);
cout << fixed << setprecision(1);
if (q-.01<=pi*4 && q+.01>=pi*4)
check=true;
else
terms++;
n++;
}
pi *= 4;
cout << terms << "        " << pi << endl;

//Part 3//

q=3.14;
n=1;
check=false;
pi = 0;
terms=0;
while (!check)
{
pi += (double) pow(-1, n+1)/(2*n-1);
cout << fixed << setprecision(2);
if (q-.001<=pi*4 && q+.001>=pi*4)
check=true;
else
terms++;
n++;
}
pi *= 4;
cout << terms << "        " << pi << endl;

//Part 4//

q=3.141;
n=1;
check=false;
pi = 0;
terms=0;
while (!check)
{
pi += (double) pow(-1, n+1)/(2*n-1);
cout << fixed << setprecision(3);
if (q-.0001<=pi*4 && q+.0001>=pi*4)
check=true;
else
terms++;
n++;
}
pi *= 4;
cout << terms << "        " << pi << endl;

//Part 5//

q=3.1415;
n=1;
check=false;
pi = 0;
terms=0;
while (!check)
{
pi += (double) pow(-1, n+1)/(2*n-1);
cout << fixed << setprecision(4);
if (q-.00001<=pi*4 && q+.00001>=pi*4)
check=true;
else
terms++;
n++;
}
pi *= 4;
cout << terms << "        " << pi << endl;

//Part 6//

q=3.14159;
n=1;
check=false;
pi = 0;
terms=0;
while (!check)
{
pi += (double) pow(-1, n+1)/(2*n-1);
cout << fixed << setprecision(5);
if (q-.000001<=pi*4 && q+.000001>=pi*4)
check=true;
else
terms++;
n++;
}
pi *= 4;
cout << terms << "        " << pi << endl;
return 0;
}``````