C++ I need help assigning values. I have to calculate pi using the infinite series so I created a loop but I'm unsure of how to assign the value to "picalcualted" so I can print it out on screen.
Not sure how correct this is but here's what I have so far. #include <iostream> #include <fstream> #include <cmath> #include <iomanip> #include <string> using namespace std; int main () { float pitrue = acos(-1); int n; cout << "Enter the accuracy to compute pi." << endl; cin >> n; cout << "Pi True" << setw(50) << "Pi Calculated" << endl; cout << setprecision (n) << pitrue << endl; double picalculated; while (fabs(pitrue-picalculated) >= pow(10.0,-1*n)); { for (int count = 1; count <= n; count++) { if (count == 1) { picalculated = 4.0; } else if (count % 2 == 1.0) { picalculated = 4.0 + (4/((2 * count) - 1.0)); } else { picalculated = 4.0 - (4/((2 * count) - 1.0)); } } } system ("PAUSE"); return 0; }
Join our real-time social learning platform and learn together with your friends!