Question

In: Computer Science

Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...

Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method

use:

C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX);

Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always evenly divisible by the number of threads.

Solutions

Expert Solution

Hello. I am giving the answer of the above question in C++. If you have any doubt in this just ask me in the comments section.


#include <bits/stdc++.h>
#define INTERVAL 10000
using namespace std;

void main()
{
   int interval; int i;
   double rand_x, rand_y, originDist, pi;
   int circle_points = 0, square_points = 0;

   srand(time(NULL));
   for (i = 0; i < (INTERVAL * INTERVAL); i++) {


       rand_x = double(rand() % (INTERVAL + 1)) / INTERVAL;
       rand_y = double(rand() % (INTERVAL + 1)) / INTERVAL;
       originDist = rand_x * rand_x + rand_y * rand_y;
       if (originDist <= 1)
           circle_points++;

square_points++;
       pi = double(4 * circle_points) / square_points;

   cout << rand_x << " " << rand_y << " " << circle_points << " " << square_points << " - " << pi << endl << endl;

if (i < 20)
           getchar();
   }
   cout << "\n Final Estimation of Pi = " << pi;


}


Related Solutions

Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
Write a matlab program that determines the value of pi using the monte carlo technique. do...
Write a matlab program that determines the value of pi using the monte carlo technique. do this for a loop of multiple fixed points. (i.e 100-10000) Plot the computed value of pi and the difference from the true value as this number increases. Time the execution of your code for various numbers of points, and plot the precision vs the computational cost.
Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is...
Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is NOT allowed.
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...
Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program accepts inputs from the user from the console. The user can provide the following two types of input: add num1 num2 mult num1 num2 Here, num1 and num2 are two integer numbers. E.g., the user may input add 1 2. The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main...
Perform Monte Carlo integration using R statistical programming to estimate the value of π. Generate N...
Perform Monte Carlo integration using R statistical programming to estimate the value of π. Generate N pairs of uniform random numbers (x,y), where x ∼ U(0,1)and y ∼ U(0,1) and each (x,y) pair represents a point in the unit square. To obtain an estimate of π count the fraction of points that fall inside the unit quarter circle and multiply by 4. Note that the fraction of points that fall inside the quarter circle should tend to the ratio between...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Develop a C++ program that looks for a given value in a text file full of...
Develop a C++ program that looks for a given value in a text file full of integer values Prompt the user for a value to search for in the file No input validation is required Open the accompanying text file named numbers.txt Search the contents of the text file You may not "hard-code" the quantity of values found in the file... Use a loop of some sort until the end of the text file is reached Maintain how many many...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Write a C program that creates 5 threads sends the thread index as an argument to...
Write a C program that creates 5 threads sends the thread index as an argument to the thread execution procedure/function. Also, the main process/thread joins the newly created threads sequentially one after the other. From the thread procedure print “I am a thread and my index is “ [print the correct index number]. From the main thread after the join print “I am the main thread and just completed joining thread index “ [print the correct index].
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT