calculate pi with loop

Have to calculate pi using the fibonacci sequence (pi = 4 - 4/3 + 4/5 - 4/7 + ...) The number of calculations or precision it goes out to is based on a user prompted for input. i.e. how many times would you like? The above example is 4 times or calculations. I think I can use a 'for' loop but not sure how to begin. Any help is much appreciated, even to just get me started, drawing a big blank on this one for some reason. A while or do..while loop is also acceptable. My output must be to 9 decimal places.
I wrote this program and it seems to work. It's hard to narrow in on the precision for Pi, and the closest I got was by entering 300001 for the number of loop iterations. I'm not sure if something funny happens with the numbers when they get that high. You can tinker with it and see if it's close to what you want.
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
#include <iostream>
#include <iomanip>

int main() {
  unsigned int decP;
  unsigned int denom=3;
  float ourPi=4.0f;
  bool addFlop=true;
  cout << "How many loop iterations? ";
  cin >> decP;
  for (unsigned int i=1;i<=decP;i++) {
    if (addFlop) {
      ourPi-=(4.0/denom);
      addFlop=false;
      denom+=2;
    } else {
      ourPi+=(4.0/denom);
      addFlop=true;
      denom+=2;
    }
  }
  cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(9);
  cout << "Pi calculated with " << decP << " iterations is: ";
  cout << ourPi << endl;
}

Enjoy!
Smart people are cool, nice people are even cooler. I guess that makes you really cool!!. Thanks for the help and the great example. I think I was trying to figure out how to make the Fibunocci sequence be exact in the code. Could not get past it! Thanks much again!!
If your still out there. I thought it would be smooth sailing but I am stuck again. I had a requirement for a user defined function for input. Seems to work fine. I also need to print out the the number of calculation/iterations as given by user (steps) i.e. if terms is 100 & steps is 10 It would calculate 100 times and print every 10th one. Instead I have messed something up in the loop.


#include <iostream>
#include <iomanip>

using namespace std;

int input(string x, int y, int z);

int main()
{

int pie, denom = 3, terms, steps;

float newPi = 4.0;

bool condition = true;

string prompt;

input( prompt, terms, steps );

cout << "Results:";
cout << endl << endl;


for ( int i = 1; i <=pie; i++ )
{
if (condition)
{

newPi -= ( 4.0 / denom );

condition = false;

denom += 2;

}

else
{

newPi += ( 4.0 / denom );

condition = true;

denom += 2;
}

cout << fixed << showpoint << setprecision(9);

cout << newPi << endl;




}



cout << endl << endl;
system ("PAUSE");
return 0;
}

int input(string x, int y, int z)
{


do {

string x = "Enter the number of terms to use: ";

cout << x;
cin >> y;
cout << endl;

if ( y <= 0 )

cout << "Error -- invalid input" << endl << endl;

}

while ( y <= 0 );

do
{
cout << "Display Pi after every how many steps? ";
cin >> z;
cout << endl;

if ( z <= 0 )

cout << "Error -- invalid input" << endl << endl;

}

while ( z <= 0);


return y;
}


See if this works right.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <string>                                  // **  add

using namespace std;

int input(string x);



int main()
{

int pie, denom = 3, terms, steps, counter;            // add counter

float newPi = 4.0;

bool condition = true;

string prompt;

terms = input( "How many terms? " );
steps = input( "Display Pi after how many steps? " );

cout << "Results:";
cout << endl << endl;

counter = 0;                                            // set counter to 0
for ( int i = 1; i <=terms; i++ )                      // ** i <= terms
{
if (condition)
{

newPi -= ( 4.0 / denom );

condition = false;

denom += 2;

}

else
{

newPi += ( 4.0 / denom );

condition = true;

denom += 2;
}

cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(9);

counter++;                                  // here we increment the counter and
if (counter == steps) {                     // see if it's time for a display step
  cout << newPi << endl;
  counter=0;
}
}



cout << endl << endl;
system ("PAUSE");
return 0;
}



int input(string x)
{
int y;
do {
cout << x;
cin >> y;
if (y<=0) cout << "Error -- invalid input" << endl << endl;

} while ( y <= 0 );

  return y;
}
closed account (z05DSL3A)
Here is your code with a few minor changes!
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void input(string& x, int& y, int& z);

int main()
{
    int denom = 3;
    int terms=0;
    int steps=0;

    double newPi = 4.0;

    bool condition = true;

    string prompt= "Enter the number of terms to use: ";

    input( prompt, terms, steps );

    cout << "Results:";
    cout << endl << endl;


    for ( int i = 1; i <= terms; i++ )
    {
        if (condition) 
        {
            newPi -= ( 4.0 / denom );
        } 
        else 
        {
            newPi += ( 4.0 / denom );
        }
        condition = !condition;
        denom += 2;

        if ((i % steps) ==0)
        {
            cout << fixed << showpoint << setprecision(9);
            cout << newPi << endl;
        }
    }

    cout << endl << endl;
    system ("PAUSE");
    return 0;
}

void input(string& x, int& y, int& z)
{
    do 
    {

        //string x = "Enter the number of terms to use: ";

        cout << x;
        cin >> y;
        cout << endl;

        if ( y <= 0 )

        cout << "Error -- invalid input" << endl << endl;

    } while ( y <= 0 );

    do 
    {
        cout << "Display Pi after every how many steps? ";
        cin >> z;
        cout << endl;

        if ( z <= 0 )

        cout << "Error -- invalid input" << endl << endl;

    }while ( z <= 0);
}


you could also look at changing your 'int's to 'unsigned long's to give you more iterations if needed.

Your input validation also needs some work.
Last edited on
Wow! many & much thanks. Two principles I have not learned yet I saw in your code. Was hoping you could enlighten. I noticed the (&) after the vars in the function 'input'. Also, when you reset, I believe, the bool 'condition', You used a (!). Thanks again. I re-ran without them just to try and figure out. Major changes.
closed account (z05DSL3A)
The & on the function arguments means that the arguments are passed by reference. This basically means that if you change the value inside the function the value that you use to call the function is changed.

See: http://www.cplusplus.com/doc/tutorial/functions2.html

The ! is a Logical not operator
!true // evaluates to false
!false // evaluates to true.

So 'condition = !condition;' sets condition to the opposite of what it is currently set to.

See: http://www.cplusplus.com/doc/tutorial/operators.html
Last edited on
Thanks again, I pulled a duh Huh! on the (!) operator. I had studied that one, just have not used much. The (&) was a complete new one. Learned some great stuff today thanks too some great mentoring. Hope I am not giving anyone a sweet tooth but, the help has been awesome!
Topic archived. No new replies allowed.