Trouble with loop program

Feb 21, 2011 at 12:18am
I have been asked to write a program that uses the formula series
∏ = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +... to calculate the value of Pi given a specified number of terms.
The example output that I was given is giving me different output numbers than the program I have written.
Can someone please take a look at my code and help me figure out why I am getting a different output than I should be.

Here is the example output that I was given:
Program will approximate Pi

Enter the number of terms to use: 5

Display Pi after every how many steps? 1

RESULTS:

   1: Pi = 4.000000000
   2: Pi = 2.666666667
   3: Pi = 3.466666667
   4: Pi = 2.895238095
   5: Pi = 3.339682540

Final Pi = 3.339682540

Press any key to continue . . .



and here is what I have so far:
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
82
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

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

int main()
{
    int denom = 3;
    int terms;
    int steps;
	int i;
    double newPi = 4.0;
    bool condition = true;


	cout << " This program will approximate Pi " << endl << endl;

    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 << steps << ": Pi = " << 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);
}
Feb 21, 2011 at 12:22am
What is the output you are getting?

I don't see anything wrong with the code at first glance.
Feb 21, 2011 at 1:44am
closed account (zb0S216C)
What compiler vendor are you using? My results are far from yours. I'm using GNU GCC by the way.
Feb 21, 2011 at 4:33am
closed account (D80DSL3A)
I ran your code and it gave the right numbers but the output form was a little off. It skips the first case and reports one extra. This is because the output follows evaluation of the next term. Moving lines 41-45 to the top of the for loop fixed this. The other problem is that every output line starts with 1: (instead of 1: then 2: then 3: , etc.). Use i instead of steps in this line cout << steps << ": Pi = " << newPi << endl; which is presently line 44.
Feb 22, 2011 at 4:06pm
Thanks so much everyone! I could not have figured that out myself.
Topic archived. No new replies allowed.