Sorry for my inexperience, i am in need of some help with my code.
My task at hand is to estimate π(Pi). The 2 required methods are the Euler series and the Gregory-Leibniz series
Rules for the code i have to use are.
• Add the statements to implement the gregory_leibniz and euler functions as described in
the program. These statements MUST be placed in the body of each function. Do not
make any changes to the main function other than adding the print statement
mentioned above.
• Do not declare any variables at the global level.
• Assume that all input values in the data file will be integers greater than zero
test file of values will have.
1
3
10000
Correct out put to show
Using 1 term(s)
Gregory-Leibniz: 4.000000000000000
Euler: 2.449489742783178
Using 3 term(s)
Gregory-Leibniz: 3.466666666666667
Euler: 2.857738033247041
Using 10000 term(s)
Gregory-Leibniz: 3.141492653590034
Euler: 3.141497163947215
My broken out put.
Using 1 term(s)
Gregory-Leibniz: 3.141591653589774
Euler: inf
Using 3 term(s)
Gregory-Leibniz: 3.141591653589774
Euler: inf
Using 10000 term(s)
Gregory-Leibniz: 3.141591653589774
Euler: inf
//The Gregory-Leibniz and Euler infinite series for estimating pi
//will be implemented as value-returning functions. The program will
//read several integers from a file that represent the number of
//terms to be used. For each data set, the program will display the
//number of terms and both estimated values with labels.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
constint L = 20; //field width of labels
constint N = 17; //field width of numbers
double gregory_leibniz(int);
double euler(int);
int main()
{
int terms; //number of terms to use in estimates
double leibniz_est; //estimated value of pi using gregory-leibniz
double euler_est; //estimated value of pi using euler
cout << fixed << setprecision(15);
cin >> terms;
while (cin)
{
cout << "Using " << terms << " term(s)" << endl;
leibniz_est = gregory_leibniz(terms);
euler_est = euler(terms);
cout << setw(L) << left << "Gregory-Leibniz: ";
cout << right << setw(N) << leibniz_est << endl;
cout << setw(L) << left << "Euler: " << right
<< setw(N) << euler_est << endl;
cin >> terms;
}
return 0;
}
double gregory_leibniz(int numofterms)
{
numofterms = N;
double pi = 0.0;
for(int N = 1 ; N <=1000000; N *=10 )
{ //calculate the results for N=1,10,...,1000000
pi = 0.0 ;
for(int i= 1 ; i<=N ; ++i)
{
int j = 2*i-1 ; //denominator term
double o = 1.0/ j ;
o = (i%2 == 1)? o : -1*o ; //Odd terms are subtracted, even terms are added
pi += o ;
}
pi = 4*pi;
}
}
double euler(int n)
//Given n terms, compute and return an estimate of pi
//using the Euler series.
{
double pi = 0.0;
for(int i = 0; i < n; ++i)
{
pi += 1.0/(i*i);
}
pi *= 6;
pi = std::sqrt(pi);
}
In Euler's function you're missing a return statement, and also you're starting the for from 0 hence why you're getting inf (1/0)
In Leibniz's function you're also missing a return statement plus, your for N loop should be removed and you're not even using numofterms argument.
Also, it explicitly said you shouldn't declare variables at global level but you did so for L and N.
You might argue they're just constants but I'd argue they're constant variables.
as a side note, 1000 terms not getting even 5 digits is pretty weak -- I think you could get better than that by counting the pixels in a big circle and dividing.
You might also be able to back it out via trig, but that is probably cheating :)