Final pi not outputting correctly

I have the following program finished except I cannot figure out why the final pi is not outputting correctly. It should take the last calculated pi and output it as the final pi. Can anyone give me an extra eye and let me know what I am doing wrong? Thanks from a newbie!

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>

using namespace std;

int returnNumber(string);


int main()
{
     double denom = 3;                                      //starting denominator at 3
     int terms=0;                                        //starting terms at 0
     int steps=0;                                           //starting steps at 0
     bool condition = true;                                 //boolean condition set at true
     double newPi = 4.0;                                    //newPi set at 4.0 to calculate changes based on user input


     cout << "Program will approximate Pi" << endl << endl;

     terms = returnNumber ("Enter the number of terms to use: ");
     steps = returnNumber("Display Pi after every how many steps?: ");


     cout << "RESULTS:";
     cout << endl << endl;

     for (int count = 1; count <= terms; count++)  //count set at 1, if count is less/equal to terms count increases

     {
          if ((count % steps) == 0)
          {
               cout << fixed << showpoint << setprecision(9);
			cout << setw(6) << count << ": Pi = " << newPi << endl;       //Pi output
          }

          if (condition)
          {
               newPi -= (4.0 / denom);               //subtracts 4/denominator depending on count
               condition = true;
          }

          else
          {
               newPi += (4.0 / denom);               //adds 4/denominator depending on count
               condition = false;
          }

          condition = !condition;
          denom += 2;

     }

     cout << endl << " Final Pi = " << newPi << endl << endl;

     system ("PAUSE");
     return 0;

}

/**************************************************
* returnNumber - Prompts user to input number of terms and steps
*
* parameter: prompt
* return: num
**************************************************/

int returnNumber(string prompt)
{
     int num;
     cout << prompt;
     cin >> num;

     while (num <=0)
     {
         cout << "ERROR - Input must be positive and non-zero! Please try again ";
         cin >> num;
     }
         cout << endl;
     return num;
}
. It should take the last calculated pi and output it as the final pi.

Yes. It seems to do that.

I tried running the program with steps=1, terms=1, got this:
RESULTS:

     1: Pi = 4.000000000

 Final Pi = 2.666666667


Clearly 4.0 is the initial value set on line 16, and 2.666666667 is the value after a single iteration. That makes sense. Using a bigger number of terms, the result homes in quite slowly on the result, which is a characteristic of this algorithm.

But all seems to be working ok, I think.
Chervil, if you use 5 for terms and 1 for steps it doesn't give out the correct final pi. But I did get some help from a fellow student and managed to get the correct results. I switched the calculation steps around and changed the starting values. The correct code is below just in case someone ever needs to use the program.

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int returnNumber(string);


int main()
{
     double denom = 1;                                      //starting denominator at 1
     int terms=0;                                        //starting terms at 0
     int steps=0;                                           //starting steps at 0
     bool condition = false;                                 //boolean condition set at false
     double newPi = 0.0;                                    //newPi set at 0.0 to calculate changes based on user input


     cout << "Program will approximate Pi" << endl << endl;

     terms = returnNumber ("Enter the number of terms to use: ");
     steps = returnNumber("Display Pi after every how many steps?: ");


     cout << "RESULTS:";
     cout << endl << endl;

     for (int count = 1; count <= terms; count++)  //count set at 1, if count is less/equal to terms count increases

     {
                    if (condition)
          {
               newPi -= (4.0 / denom);               //subtracts 4/denominator depending on count
          }

          else
          {
               newPi += (4.0 / denom);               //adds 4/denominator depending on count
          }

          condition = !condition;                      //changes sign
          denom += 2;                                  //increments for denominator

          if ((count % steps) == 0)
          {
               cout << fixed << showpoint << setprecision(9);
			cout << setw(6) << count << ": Pi = " << newPi << endl;       //Pi output
          }

     }

     cout << endl << " Final Pi = " << newPi << endl << endl;

     system ("PAUSE");
     return 0;

}

/**************************************************
* returnNumber - Prompts user to input number of terms and steps
*
* parameter: string prompt
* return: int num
**************************************************/

int returnNumber(string prompt)
{
     int num;
     cout << prompt;
     cin >> num;

     while (num <=0)
     {
         cout << "ERROR - Input must be positive and non-zero! Please try again ";
         cin >> num;
     }
         cout << endl;
     return num;
}
Topic archived. No new replies allowed.