Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

An interesting way of calculating a is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows: Suppose

An interesting way of calculating π is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows: Suppose you have a circle inscribed within a square, as shown in Figure below (Assume that the radius of this circle is 1.) First, generate a series of random points as simple (x, y) coordinates. These points must fall within the Cartesian coordinates that bound the square. Of the total number of random points that are generated, some will occur within the circle. Next, estimate π by performing the following calculation: π = 4 x (number of points in circle)/(total number of points) Write a multithreaded version of this algorithm in C/C++ that creates a separate thread to generate a number of random points. The thread will count the number of points that occur within the circle and store that result in a global variable. When this thread has exited, the parent thread will calculate and output the estimated value of. It is worth experimenting with the number of random points generated. As a general rule, the greater the number of points, the closer the approximation to π. 

An interesting way of calculating a is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows: Suppose you have a circle inscribed within a square, as shown in Figure below (-1, 1) (1, 1) (0, 0) (-1, -1). (1,-1) (Assume that the radius of this circle is 1.) First, generate a series of random points as simple (x, y) coordinates. These points must fall within the Cartesian coordinates that bound the square. Of the total number of random points that are generated, some will occur within the circle. Next, estimate a by performing the following calculation: TT = 4 x (number of points in circle)/(total number of points) Write a multithreaded version of this algorithm in C/C++ that creates a separate thread to generate a number of random points. The thread will count the number of points that occur within the circle and store that result in a global variable. When this thread has exited, the parent thread will calculate and output the estimated value of t. It is worth experimenting with the number of random points generated. As a general rule, the greater the number of points, the closer the approximation to T. A code segment for generating random numbers, as well as determining if the random (x, y) point occurs within the circle is provided below: /* Generates a double precision random number */ double random_double () { return random () / ( (double) RAND MAX + 1); } /* Check for points inside circle for (i = 0; i < npoints; i++) { /* generate random numbers between -1.0 and +1.0 (exclusive) */ x = random double () * 2.0 - 1.03; y = random_double () * 2.0 - 1.0; if (sqrt (x*x + y*y) < 1.0 ) ++hit_count; } Name the program mcarlo.c or mcarlo.cpp and provide the number of points of the command line: > . /mcarlo Use at least two worker threads to calculate the total number of points as well and the number of points inside the circle. Error Handling Perform the necessary error checking to ensure the correct number of command-line parameters. Verify the number of points is a valid number and during test vary signficantyl the number of points to see the difference in the accuracy of the estimation of TT.

Step by Step Solution

3.57 Rating (154 Votes )

There are 3 Steps involved in it

Step: 1

include For random RANDMAX include include define total 1200 int checkvaluei... blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Structures and Algorithms in Java

Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser

6th edition

1118771334, 1118771338, 978-1118771334

More Books

Students explore these related Finance questions

Question

Solve each equation. x 3 - 6x 2 = -8x

Answered: 3 weeks ago