Have to calculate pi using the fibonacci sequence (pi = 4 - 4/3 + 4/5 - 4/7 + ...) The number of calculations or precision it goes out to is based on a user prompted for input. i.e. how many times would you like? The above example is 4 times or calculations. I think I can use a 'for' loop but not sure how to begin. Any help is much appreciated, even to just get me started, drawing a big blank on this one for some reason. A while or do..while loop is also acceptable. My output must be to 9 decimal places.
I wrote this program and it seems to work. It's hard to narrow in on the precision for Pi, and the closest I got was by entering 300001 for the number of loop iterations. I'm not sure if something funny happens with the numbers when they get that high. You can tinker with it and see if it's close to what you want.
Smart people are cool, nice people are even cooler. I guess that makes you really cool!!. Thanks for the help and the great example. I think I was trying to figure out how to make the Fibunocci sequence be exact in the code. Could not get past it! Thanks much again!!
If your still out there. I thought it would be smooth sailing but I am stuck again. I had a requirement for a user defined function for input. Seems to work fine. I also need to print out the the number of calculation/iterations as given by user (steps) i.e. if terms is 100 & steps is 10 It would calculate 100 times and print every 10th one. Instead I have messed something up in the loop.
#include <iostream>
#include <iomanip>
using namespace std;
int input(string x, int y, int z);
int main()
{
int pie, denom = 3, terms, steps;
float newPi = 4.0;
bool condition = true;
string prompt;
input( prompt, terms, steps );
cout << "Results:";
cout << endl << endl;
for ( int i = 1; i <=pie; i++ )
{
if (condition)
{
newPi -= ( 4.0 / denom );
condition = false;
denom += 2;
}
else
{
newPi += ( 4.0 / denom );
condition = true;
denom += 2;
}
cout << fixed << showpoint << setprecision(9);
cout << newPi << endl;
}
cout << endl << endl;
system ("PAUSE");
return 0;
}
int 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;
Wow! many & much thanks. Two principles I have not learned yet I saw in your code. Was hoping you could enlighten. I noticed the (&) after the vars in the function 'input'. Also, when you reset, I believe, the bool 'condition', You used a (!). Thanks again. I re-ran without them just to try and figure out. Major changes.
The & on the function arguments means that the arguments are passed by reference. This basically means that if you change the value inside the function the value that you use to call the function is changed.
Thanks again, I pulled a duh Huh! on the (!) operator. I had studied that one, just have not used much. The (&) was a complete new one. Learned some great stuff today thanks too some great mentoring. Hope I am not giving anyone a sweet tooth but, the help has been awesome!