After informing the user what the program does prompt the


Assignment

After informing the user what the program does, prompt the user for the number of darts to throw. Also, prompt for a number of simulations (why not have the program simulate this dart throwing more than once). Then, for each "dart" to be thrown, generate a random x and y coordinate within this region. Calling the function rand() will give you a random number 0...INT_MAX. If you divide the number by INT_MAX, you'll be able to generate a random number in the interval [0,1). Remember to seed the random generator. After these random coordinates are generate, (i.e., a dart is thrown), see if the dart falls within the unit quarter-circle. Since the radius of the unit circle is 1, to determine if the dart is inside simply calculate the distance from the origin to the coordinate. If the distance is less than one, the dart must be inside the quarter circle. Otherwise, it falls outside this region. If the dart is inside the quarter circle, keep track of this by incrementing a count variable, say dartsInside. Once all the darts are thrown, divide the dartsInside by the total number of darts thrown to approximate the area of the quarter circle.

This has now successfully made an approximation of pi/4, but was only a throw of n darts. Your program is going to simulate throwing n darts m times. Just reset the count variable and run the approximation again. Each simulation will generate a new approximation of pi/4. We'll take the average of all these approximations to give our final approximation of pi. Normally, the average of m numbers is the sum of the data divided by m. However, in order to avoid overflow, convince yourself of the equality

a1+a2+...+am / m = a1 / m + a2 / m + ... am/m

Thus, instead of accumulating the approximations of pi, your program will accumulate the approximation divided by m. Once all the simulations are finished, multiply your value by four and print the programs approximation of pi.

Finally, to see different approximations, allow the user to repeat this process of throwing n darts m simulated times an indefinite number of times-until the user enters in a value indicated to quit. For example, the sentinel value could be 0. This program will also guard against bad data. When the user enters in a number, the number could be invalid - i.e., less than 0, or it could be a character. In either event, your program should handle this without crashing, recover from this bad data and continue execution.

Solution Preview :

Prepared by a verified Expert
Programming Languages: After informing the user what the program does prompt the
Reference No:- TGS01726513

Now Priced at $40 (50% Discount)

Recommended (91%)

Rated (4.3/5)