Final Pi giving undesired output
Feb 26, 2011 at 5:54pm UTC
This program is all but finished I am just having trouble figuring out how to display the last term in my sequence as a separate output after the other terms have been outputted.
I want it to give however many results and then the last result coppied and displayed as final pi.
Example:
Result:
100: Pi = 3.131592904
200: Pi = 3.136592685
300: Pi = 3.138259330
Final Pi = 3.138259330
So what I am trying to do is have the 300 result # display 2 lines down on the final pi =.
This is what I have and am having trouble with line 54; please help:
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 83 84 85 86 87 88 89 90 91 92
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int call_prompt;
int store_terms;
int store_steps;
void input(string& call_prompt, int & store_terms, int & store_steps);
int main()
{
int denominator = 3;
int num_terms;
int num_steps;
int pi_num;
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, num_terms, num_steps );
cout << " Results:" ;
cout << endl << endl;
for ( int pi_num = 1; pi_num <= num_terms; pi_num++ )
{
if ((pi_num % num_steps) == 0)
{
cout << fixed << showpoint << setprecision(9);
cout << setw(6) << pi_num << ": Pi = " << newPi << endl;
}
if (condition)
{
newPi -= ( 4.0 / denominator );
}
else
{
newPi += ( 4.0 / denominator );
}
condition = !condition;
denominator += 2;
}
cout << endl << endl;
cout << " Final Pi = " << newPi;
cout << endl << endl;
system ("PAUSE" );
return 0;
}
void input(string& call_prompt, int & store_terms, int & store_steps)
{
do
{
//string call_prompt = "Enter the number of terms to use: ";
cout << call_prompt;
cin >> store_terms;
cout << endl;
// if number of terms stored = 0 output error message
if ( store_terms <= 0 )
cout << " Error -- invalid input" << endl << endl;
} while ( store_terms <= 0 );
do
{
cout << " Display Pi after every how many steps? " ;
cin >> store_steps; //store a value for number of steps
cout << endl;
if ( store_steps <= 0 ) //gives a condition to follow if stored value is <= 0
cout << " Error -- invalid input" << endl << endl;
}while ( store_steps <= 0); //condition to follow if steps is an exeptable #
}
Topic archived. No new replies allowed.