Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A An interesting way of calculating Pi is to use a technique known as Monte

ID: 3763342 • Letter: P

Question

Part A An interesting way of calculating Pi 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 the following figure (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 Pi by performing the following calculation: Pi = 4 *( number of points in circle )/( total number of points ) Write a multithreaded C program of this algorithm 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 Pi . 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 Pi . So the total number of points generated by the created thread should be no less than 50,000,000. Hint: You may use the following function to generate a double precision random number between 0 and 1: double random_double() { return random() / ((double)RAND_MAX + 1); } You may use the following formula to determine if random point (x, y) occurs within the circle: ( sqrt(x*x + y*y) < 1.0) where the formula gives true if random point (x, y) occurs within the circle, otherwise it gives false. In addition, you need to include the "stdlib.h" and "math.h" in your code to use the provided function and formula. When you compile your code, you need to add additional parameter "-lpthread " and "-lm " as the following example: gcc YourCode.c -lpthread -lm Part B Now we extend the Part A so that you create several threads, each of which generates random points and determines if the points fall within the circle. Each thread will have to update the global count of all points that fall within the circle. Protect against race conditions on updates to the shared global variable by using mutex locks. The number of created threads will be provided from the command line. Perform necessary error checking to ensure that a positive integer is passed on the command line. The total number of points generated by all the created threads should be no less than 50,000,000. Following are some running examples, assuming the compiled program named b.out: ./b.out Usage: ./b.out ./b.out 10 20 Usage: ./b.out ./b.out -3 should be a positive integer ./b.out 10 Pi = 3.141446

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <stdlib.h>
int i = 0;  
int amt_Wanted = 0;
int total_Pts = 0;
void *count(void *X)
{
  
for (i=0; i < amt_Wanted; i++)
{  
double X = (double)rand() / RAND_MAX;
double Y = (double)rand() / RAND_MAX;
  
if (((X * X) + (Y * Y)) <= 1)
{
total_Pts++;
}
}
return NULL;
}

int main()
{
printf(" hreaded Monte Carlo ");
srand(time(NULL));
pthread_t thread;

do
   {
printf(" enter a positive number for the amount of points you would like to generate? ");
scanf("%d", &amt_Wanted);    
   }while (amt_Wanted <= 0);
  
pthread_create(&thread, NULL, &count, NULL);
pthread_join(thread, NULL);
  
double points = 4.0 * total_Pts;
double pi = points / amt_Wanted;
printf("approximate value of pi for the desired amount of points (%d) is: %f ", amt_Wanted, pi);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote