// Eric Berger // October 30, 2005 // ENGR 021 Monday 6:30-9:40 PM // PURPOSE: Approximating 1/3 Pi // PSEUDOCODE: /* 1. open file 2. do a loop a thousand times. 3. run equation and have it add each time it runs. 4. add and subtract alternating values. 4. print out the first 100 numbers 5. then print out the next 10 out of a 1000 6. then print out every 250th number. 9. colse program */ #include #include #include #include using namespace std; int main() { int i=0; // "i" is the number that increments in the queations. int j=1; // "j" i need for incrementing by 2. int n=1; // "n" if you switching signs on the answer. int counter=0; // to show that number in the series is printed. double Pi_calc=0; // is for the placeholder value. double pi=0; // to dump final result into. ofstream outData ("c:\\Approximating_Pi.txt"); //opens file to print to cout << "There are many formulas for computing pi. One of the simplest is: \n" << endl; cout << " Run Value\n"; outData << " Run Value\n"; for(i = 0; i < 10001; i++) // loops equation 1000 times { if (i==0) // dumps first term {pi=1;} else if (i%2==0) // dumps every other term {pi=pow(1,n)/j;} else {pi=-pow(1,n)/j;} // dumps odd terms Pi_calc+=pi; // add values up if ( i < 100 ) // prints first 100 terms { cout << fixed << setw(5) << counter << setw(15) << Pi_calc << endl; outData << fixed << setw(5) << counter << setw(15) << Pi_calc << endl; } else if (i%10==0 && i<=1000) // prints every 10th term after the first 100 { cout << fixed << setw(5) << counter << setw(15) << Pi_calc << endl; outData << fixed << setw(5) << counter << setw(15) << Pi_calc << endl; } else if (i%250==0 && i>1000) // prints every 250th term after the first 100 { cout << fixed << setw(5) << counter << setw(15) << Pi_calc << endl; outData << fixed << setw(5) << counter << setw(15) << Pi_calc << endl; } j+=2; // increments j by 2 counter++; // bumps counter up by 1 } return 0; }