
July 11th, 2017, 04:14 PM
#1
Approximating Pi?
I'm trying to write a program that will approximate pi based on a simulation of darts being thrown for a class.
This is what the template code looked like for the assignment:
Code:
#include <iostream> //Required for cin, cout
#include <iomanip> //Required for setting precision
#include <cstdlib> //Required for random numbers
#include <ctime> //Required for system clock reading
using namespace std;
int main()
{
// Declare and initialize objects.
int n; // input  number of dart throws to simulate
double pi; // output  approximation of pi
double x, y; // working  coordinates of dart “throw”
srand(time(0)); //use current time as seed
for ( int count = 1; count <= n; count++ ) {
x = (double) rand()/RAND_MAX;
y = (double) rand()/RAND_MAX;
} //end for loop
// Exit program.
return 0;
This is what I've added in, but I'm not sure what to add in for "pi" on line 23. Any help?
Code:
#include <iostream> //Required for cin, cout
#include <iomanip> //Required for setting precision
#include <cstdlib> //Required for random numbers
#include <ctime> //Required for system clock reading
using namespace std;
int main()
{
//Declare and initialize objects
int n; //input  number of dart throws to simulate
double pi; //output  approximation of pi
double x, y; //working  coordinates of dart "throw"
cout << "How many dart throws would you like to be simulated?" << endl;
cin >> n;
srand(time(0)); //use current time as seed
for ( int count = 1; count <= n; count++ ) {
x = (double) rand()/RAND_MAX;
y = (double) rand()/RAND_MAX;
pi =;
cout << "The approximate value of pi is: " << pi << endl;
} //end of loop
return 0;
}

July 11th, 2017, 04:39 PM
#2
Re: Approximating Pi?
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C, C++ Compiler: Microsoft VS2017.2

July 13th, 2017, 07:16 AM
#3
Re: Approximating Pi?
FYI: PI can be precisely evaluated by factorization using Taylor's series.
Popular opinion is the greatest lie in the world.

July 13th, 2017, 08:22 AM
#4
Re: Approximating Pi?
PI can be precisely evaluated
As PI is an irrational number there is no exact value for it (its decimal representation never ends and never settles into a permanent repeating pattern). There are various convergent series that can be used to obtain a value of pi to the required accuracy depending upon the number of terms used in the series. Depending upon the series used, depends upon how rapidly the series sum converges to the required accuracy. Currently the value of pi is known to over 10 trillion digits. There are many series that converge more rapidly than the Taylor series (PI/4 = 1  1/3 + 1/5  1/7 + ..) eg the Chudnovsky formula. See https://en.wikipedia.org/wiki/Pi#Mod...or_more_digits for more info.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C, C++ Compiler: Microsoft VS2017.2

July 13th, 2017, 08:30 AM
#5
Re: Approximating Pi?
Thanks for informative answer. By saying precisely, I emphasized the fact that it can be calculated with a customized precision which is in most of the cases is acceptable x86 and/or x64 architectures , as the OP means approximation, I thought the method should work out without a hassle.
Popular opinion is the greatest lie in the world.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
