calculating Pi w/ dart throws


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: calculating Pi w/ dart throws

  1. #1
    Join Date
    Jul 2005
    Posts
    1

    calculating Pi w/ dart throws

    I need help with the void doDartExperiment. I have no idea how to call the class in the program. I wrote everything else, granted the rest is easy. Just some help or a push in the right direction.

    PHP Code:
    The value for p can be approximated empiricallythat isby carrying out practical experimentsOne such experiment is described as follows
     
    We use a quarter unit circle centered at the origin and located inside the unit square on the first quadrantUsing random numbers between 0.0 and 1.0 (the "unit interval"), dart "throws" are generatedEach "throw" is an ordered pair (y), where x and y are randomly generated numbers from the unit intervalThose that fall inside the unit circle (the "hits"are tabulatedThe value for pi is then computed using the formula
    4   noof hits noof throws 

    The required solution makes use of functions and classesYou should have a function that accepts and validates input from the userfunction that undertakes the experiment, and yet another that presents the result of the experiment to the userThe suggested function units are as follows:

    void readNumberOfThrowsint);
      
    //this reads and validates n,
      //the number of dart throws involved in the experiment
    void doDartExperimentint ndoublepi );
      
    //this is the actual experiment where n is the number of throws,
      //and pi is the resulting approximation
    void displayResultint ndouble pi) ;
      
    //this displays the results of the experiment

    To model the dart throwsclass needs to be implemented.  The class declaration is as follows (to be stored in file DartThrow.h):

    #include <iostream>
    using namespace std;
    class 
    DartThrow {
      
    //class body
      
    public:
        
    DartThrow();  //constructor
        
    void print( ostream& ) const;  //output method
        
    double getX() const;  // access methods
        
    double getY() const;
      private:
        
    double random();  //generates a random double value between 0.0 and 1.0
        
    double xy;  //components of the dart throw
    };

    You need to provide the implementation of the constructorthe output method, and the access methods (to be stored in file DartThrow.cpp).  Since the dart throw is randomly produced (i.e., the x- and y-coordinates are random values between 0.0 and 1.0), it should be obvious that the constructor should invoke the (private) random() function.  The following technique to generate random double values in the unit interval makes use of an exercise performed in Lab 4:


    double random() {
      return 
    rand_double(0.0,1.0);
    }

    In the implementation of the function doDartExperimentint ndoublepi ), n DartThrow objects should be generated using loop logic and of thesedetermine how many are "hits."  


    A sample run of the required program is as follows:


    PI APPROXIMATION PROGRAM

    How many dart throws should be simulated
    ?
    1000

    Number of dart throws simulated
    1000
    Approximate value of pi is
    3.1120000000

    In the above 
    "trace" of the executionthe red-colored number 1000 is typed in by the userThe value 3.1120000000 is the approximation of p achieved by simulating the throwing of 1000 dartsYour program should produce such a "dialogue" with the user when executed and display the approximation for p with 10 decimal digits.

    If 
    the input is less than 1the program should continue prompting for another value until such a time as the user types in a number greater than 0:

    PI APPROXIMATION PROGRAM

    How many dart throws should be simulated
    ?
    -
    5

    Input should be at least 1
    ! Try again.

    How many dart throws should be simulated?
    1000000

    Number of dart throws simulated
    1000000
    Approximate value of pi is
    3.139088 

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    what exactly is you problem?
    To call an ordinary function like void doDartExperiment(...)
    just do sth like:

    int main(int argc, char* argv[])
    {
    DartThrow dt;
    dt.getX();
    dt.getY();
    /*... */
    return 0;
    }

    regards,

    D

Similar Threads

  1. Managed server throws error on start up
    By greg_michael20 in forum Java
    Replies: 0
    Last Post: 05-31-2005, 07:17 AM
  2. Calculating Fiscal Year in SQL?
    By James McFarlane in forum Database
    Replies: 1
    Last Post: 04-08-2002, 07:49 AM
  3. Problem in calculating WEEK OF YEAR
    By myproblem in forum Java
    Replies: 1
    Last Post: 12-06-2001, 11:15 AM
  4. Calculating
    By Gus in forum Database
    Replies: 4
    Last Post: 04-23-2001, 10:32 AM
  5. Calculating Buisness Week
    By Thomas in forum Database
    Replies: 5
    Last Post: 11-28-2000, 11:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center