Monday, May 24, 2010

How do I make PI more accurate when inputting # of terms to calculate in C++?

It seems to be correct except that it outputs 4 as the answer everytime.





#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


#include %26lt;string%26gt;


using namespace std;


double pi_one(int accuracy_func);


int accuracy=0;


long delta_input=0;


double show_pi=0;





int main( )


{


while(true){


cout %26lt;%26lt; "How accurate do you want me to calculate PI for you, i.e. # of terms:\n";





cin %26gt;%26gt; accuracy;


show_pi = pi_one(accuracy); //Invoke function #1





cout %26lt;%26lt; "Here you go. Have a nice day!\n" %26lt;%26lt; show_pi %26lt;%26lt; endl;








string ans = "x";


cout %26lt;%26lt; "Do you want to continue? (y/n)\n";


cin %26gt;%26gt; ans;





if(ans=="y"){


cout %26lt;%26lt; endl %26lt;%26lt; endl %26lt;%26lt; "Let's do this again!\n" %26lt;%26lt; endl;


}





else if(ans=="n"){


cout %26lt;%26lt; "SEEEEEE you LATERRRR...\n";


return 0;





}





}


}





double pi_one(int accuracy_func)


{


double pi_total=0;





while(true){


for(double i=0; i%26lt;accuracy; i++){





pi_total += pow(-1.0,i)*(4/(2*i+1));





return (pi_total);


}





}


}

How do I make PI more accurate when inputting # of terms to calculate in C++?
Maybe I don't see it, but your while loop in main, when does it become False??? It calls the function but that also is while true.


Just do the for loop there.





I did the equation and it returns different answers. but the While Loops should not be like that.





While(ans=='y')


No comments:

Post a Comment