Approximating Pi?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Approximating Pi?

  1. #1
    Join Date
    Jul 2017
    Posts
    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;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,719

    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++17 Compiler: Microsoft VS2017 (15.3.4)

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: Approximating Pi?

    FYI: PI can be precisely evaluated by factorization using Taylor's series.
    Popular opinion is the greatest lie in the world.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,719

    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++17 Compiler: Microsoft VS2017 (15.3.4)

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)