Pi estimation problem

Write your question here.
//Mathematician Gottfried Wilhelm Leibnitz wrote this equation to estimate pi, pi = sqrt(12)*(1-(1/(3*3)+(1/(5*3^2)-(1/(7*3^3))+G , with G as the number of iterations. Essentially what the question is asking of me is to code a program that prompts the user to enter tolerance, use a while loop to converge on solution to 'equal' pi within tolerance and the least number of iterations possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>


using namespace std;
const double MPi  =3.141592653589793238463;
double Pi_est, Tol, G; //estimate, Tolerance, G is the iteration increase
int i,p,j, k; // the interations increases except k which the iteration counter


int main(int nNumberofArgs, char*pszArgs[]){
    cout<<"Please enter desired tolerance 'any whole interger': ";
    cin>>Tol;
    Tol = Tol/10;
    cout<<MPi;

    for(k=0;k<100;k++){         //for counter loop
        cout<<k;                    // to check loop
     while (Tol < abs((Pi_est-MPi)/MPi)){  // while equation not within tolerances
    }    for (i = 3;i<MPi;i++){
    }
    for ( j=7;j<15;j++){
    }
    for ( p = 3;p<6;p++){
       }
     if (Tol > abs((Pi_est-MPi)/MPi)){
         break;
         if(Tol != abs((Pi_est-MPi)/MPi)){
             G =(1/(j+i^p));
}         else
          Pi_est = sqrt(12)*(1-(1/9)+(1/45)-(1/(7*27) + G));
    }
            if (k>10){
                cout<<"this is tolerance"<<Tol<<endl;
                cout<<"the is the estimate"<<Pi_est<<endl;
                break;}

cout<<"this is the iterations";
return k;
}
}



this is what I have so far, the problem I am having is that the execution problem freezes and will not complete the loop. My guess is that it can't due to the fact the Tolerance is never reached, I'm not sure. I also apologize for any grammar or spelling errors.
Could you give a link to the source of that equation? (Websearch yields a different equation.)
To be fair, there are a lot of iterative equations for estimating pi so it is no guarantee that just because it isn't near the top of google, it isn't valid.
You seem to be closing all of your loops when they should not be closed.

You want braces { } surrounding the code blocks not before.

It probably freezes because
1
2
while (Tol < abs((Pi_est-MPi)/MPi)){  // while equation not within tolerances
    } 
is going to iterate forever unless it is true the first time, or until the program crashes.

Please read: http://www.cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.