HELP! calculate Pi with dart throws
I can't figure out how to write or start the void doDartExperiment. Should be simple, I'm just baffled. Any help would be great.
Here's the assignment:
The value for pi can be approximated empirically, that is, by carrying out practical experiments. One 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 quadrant. Using random numbers between 0.0 and 1.0 (the "unit interval"), dart "throws" are generated. Each "throw" is an ordered pair (x , y), where x and y are randomly generated numbers from the unit interval. Those that fall inside the unit circle (the "hits") are tabulated. The value for pi is then computed using the formula:
p = 4 × ( no. of hits / no. of throws )
The required solution makes use of functions and classes. You should have a function that accepts and validates input from the user, a function that undertakes the experiment, and yet another that presents the result of the experiment to the user. The suggested function units are as follows:
void readNumberOfThrows( int& n );
//this reads and validates n,
//the number of dart throws involved in the experiment
void doDartExperiment( int n, double& pi );
//this is the actual experiment where n is the number of throws,
//and pi is the resulting approximation
void displayResult( int n, double pi) ;
//this displays the results of the experiment
To model the dart throws, a class 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 x, y; //components of the dart throw
};
You need to provide the implementation of the constructor, the 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 doDartExperiment( int n, double& pi ), n DartThrow objects should be generated using loop logic and of these, determine 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 execution, the red-colored number 1000 is typed in by the user. The value 3.1120000000 is the approximation of p achieved by simulating the throwing of 1000 darts. Your 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 1, the 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
Quote:Original post by TojiThat's exactly what he said it is.
This sounds a lot like a homework assignment. Is it?
It's all very straightforward, but we are unable to help you here.
I strongly suggest you read the assignment through, and then stop and think about the problem. Just because you don't know the anser straight off doesn't mean it wont come to you after a few minutes.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement