In C xSpicewerks OpenTi en TicketsxMy Schedule SAF NetWex SA

In C,

xSpicewerks- OpenTi en TicketsxMy Schedule- SAF NetWe..x SAP NetWe\" SAF NetWener Porte! Courses-Blackboard Learn / Open Tickets × Part A An Interesting way .. x nttps:/blackbosrd.olemiss.edubswebdawipid-1832234-dt-cortent-rid-42086848 1/coures/Csci 423 Secticn1 WANG 2015-2017 FALL/ProgrammingAssignmenn4 pdt G Allm Blackbeard Home Pa. Spiceworks Open Tic.··b SurveyMonkey: Freee ) Citrix Secure Signin-Asagnments-Csci 42 Page 1 of 2 - + Aut amatic Zoem CSCI423 Programming Assignment 4 Part A 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 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, est inate by performing the following calculation: = 4x (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 . lt 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 So the total number of points generated by the created thread should be no less than 50,000,000. 8:45 AM 10//7016

Solution

/************************ PART A STARTS*******************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<pthread.h>

#define MIN_POINT_COUNT 50000000//Minimum number of coordinates to be generated

struct coordinates//Data type to store each coordinate
{
   double x;
   double y;
};

void *thread(void *p);//Prototype of thread to generate random points and count number of points inside circle
struct coordinates random_double();//Function to generate random coordinates

unsigned int total_points_in_circle=0;//Global Variable to hold total points inside the circle
unsigned int total_points;//Total points to be generated

int main()
{
  
   pthread_t t;
   pthread_create(&t,NULL,thread,NULL);//Create thread
   pthread_join(t,NULL);//Waiting for the thread to exit
  
   double pi = 4 * total_points_in_circle / (double)total_points;//calculating the value of pi
   printf(\"Value of pi = %lf\ \",pi);//printing the value of pi
  
  


}

void *thread(void *p)
{
   total_points = random()%MIN_POINT_COUNT+MIN_POINT_COUNT;//Generating the random number which is greater than MIN_POINT_COUNT (50000000)
   unsigned int i;
   struct coordinates rand_co;
   for(i=0;i<total_points;i++)
   {
       rand_co = random_double();//Generating the random coordinate and store
       if(sqrt(rand_co.x*rand_co.x + rand_co.y*rand_co.y) < 1.0)// Checking whether given random coordinate is inside the circle or not
           total_points_in_circle++;
      
   }  

   pthread_exit(NULL);//Exiting the thread
}

struct coordinates random_double()
{
   struct coordinates rand_co;
   rand_co.x = random() / ((double)RAND_MAX +1);//Generating the double value which is in between 0 to 1.0
   rand_co.y = random() / ((double)RAND_MAX +1);
   return rand_co;
}

/************************************PART A END**********************************/

/****************************PART B STARTS*************************************/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<pthread.h>

#define MIN_POINT_COUNT 50000000//Minimum number of coordinates to be generated

struct coordinates//Data type to store each coordinate
{
   double x;
   double y;
};

void *thread(void *p);//Prototype of thread to generate random points and count number of points inside circle
struct coordinates random_double();//Function to generate random coordinates

unsigned int total_points_in_circle=0;//Global Variable to hold total points inside the circle
unsigned int per_thread_total_points;//Per thread Total points to be generated
unsigned int total_points;
pthread_mutex_t mutex;//Mutex object

int main(int argc,char **argv)
{
   if(argc == 1)//Checking whether 2 arguments are passed or not
   {
       printf(\"Provide the number of threads to be created in the command line....\ \");
       return 1;
   }
  
   int thread_count = atoi(argv[1]);//converting string to integer
   if(thread_count < 0)//Checking whether thread count is less than 0 or not
   {
       printf(\"Provide positive number in the commandline....\ \");
       return 1;
   }
  
   if(pthread_mutex_init(&mutex,NULL) != 0)// Initializing the mutex structure
   {
       printf(\"Mutex init is failed....\ \");
       return 1;
   }

   per_thread_total_points = random()%MIN_POINT_COUNT+MIN_POINT_COUNT;//Generating the random number which is greater than MIN_POINT_COUNT (50000000)
  
   total_points = thread_count * per_thread_total_points;//Total points by summing the points of all threads
   pthread_t *t = malloc(sizeof(pthread_t)*thread_count);//Allocating thread structures dynamically of size thread_count
   int i = 0;

   for(i=0;i<thread_count;i++)//Creating threads of size thread_count
   {
   pthread_create(&t[i],NULL,thread,NULL);//Create thread
   }

   for(i=0;i<thread_count;i++)
   {
   pthread_join(t[i],NULL);//Waiting for the thread to exit
   }

   double pi = 4 * total_points_in_circle / (double)total_points;//calculating the value of pi
   printf(\"Value of pi = %lf\ \",pi);//printing the value of pi
  
  


}

void *thread(void *p)
{
   unsigned int i;
   struct coordinates rand_co;
   for(i=0;i<per_thread_total_points;i++)
   {
       rand_co = random_double();//Generating the random coordinate and store
       if(sqrt(rand_co.x*rand_co.x + rand_co.y*rand_co.y) < 1.0)// Checking whether given random coordinate is inside the circle or not
       {
           pthread_mutex_lock(&mutex);//Holding the mutex
           total_points_in_circle++;
           pthread_mutex_unlock(&mutex);//Releasing the mutex lock
       }
          
      
   }  

   pthread_exit(NULL);//Exiting the thread
}

struct coordinates random_double()
{
   struct coordinates rand_co;
   rand_co.x = random() / ((double)RAND_MAX +1);//Generating the double value which is in between 0 to 1.0
   rand_co.y = random() / ((double)RAND_MAX +1);
   return rand_co;
}

/****************************PART B ENDS****************************************/

In C, xSpicewerks- OpenTi en TicketsxMy Schedule- SAF NetWe..x SAP NetWe\
In C, xSpicewerks- OpenTi en TicketsxMy Schedule- SAF NetWe..x SAP NetWe\
In C, xSpicewerks- OpenTi en TicketsxMy Schedule- SAF NetWe..x SAP NetWe\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site