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

in C programming with output matching the bottom examples For this problem you w

ID: 3594988 • Letter: I

Question

in C programming with output matching the bottom examples

For this problem you will be implementing a Monte Carlo method for the estimation of pi. Imagine that you have a circle of radius 1 whose center is at (0,0) surrounded by a square with sides of length 2 as pictured below

If we randomly choose points within the square the probability of a point being inside the circle is proportional to the area of the square that it occupies. This means that

The probability for pi can be approximiated by picking a large number of points between [-1, 1] and counting the number that are inside the circle. The number of points inside the circle divided by the the total number of points picked is the probability of being inside the circle

Report your answer to 3 digits after the decimal place

Assumptions

Input will not always be valid

If invalid input is received your program should continue to ask for more input until a valid value is entered

White space after desired input is allowed

You will probably want to wait until after we cover how to do this in class before handling it

Valid values for inputs

Seed: an integer

Number iterations: an integer greater than 0

Randomness

you will need to make calls to rand in the exact order that I do

First generate a random number between [-1, 1] for the x axis

Then generate a random number between [-1,1] for the y axis

You should the following expression for generating a random double between LO and HI

LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO))

Hint it would be a good idea to make the above expression into a function

Numbers that fall exactly on the edge of the circle are considered to be inside the circle

Examples

output should match the examples

Example 1

Enter the seed for the random number generator: 4
Enter the number of iterations to run: 10
The value of pi is 3.200.

Example 2

Enter the seed for the random number generator: -7
Enter the number of iterations to run: 2
The value of pi is 4.000.

Example 3

Enter the seed for the random number generator: -7
Enter the number of iterations to run: 10000
The value of pi is 3.144.

Example 4

Enter the seed for the random number generator: 3.45
Enter the seed for the random number generator: why
Enter the seed for the random number generator: 12
Enter the number of iterations to run: cat
Enter the number of iterations to run: -1
Enter the number of iterations to run: 3.141
Enter the number of iterations to run: 75
The value of pi is 3.147.

Requirements

Use doubles to store real numbers

Print all doubles to 2 decimal points

Restrictions

No global variables may be used

Your main function may only declare variables and call other functions

(0,0) (0-1)

Explanation / Answer

********************************************************************

The sample input and output match as given in the question.

********************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Function to generate random numbers
double generateRandom(int seed) {

    int LO=-1;
    int HI=1;
    double r = LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO));
    return r;

}
//Function to take input and validate
int input(int *b){
   char input[100] = "";
   double x;
   int num;
   char str[20] = "";
   int assignment[5] = {0};
   double tolerance = 1e-12;
   fgets(input, 100, stdin);
   if (sscanf(input, "%lf", &x) == 1) {
      num = (int)x;
      if (fabs(x - num)/x > tolerance ) {
         return -1;
      } else {
         *b=1;
         return num;
      }
   } else if (sscanf(input, "%s", str) == 1) {
         return -1;
   } else {
      return -1;
   }
}
//Function to calculate PI
double calculatePI(int seed, int iterations) {
    double total=iterations;
    double inside=0;
    double x,y;
    for(int i=1;i<=iterations;i++) {
        x=generateRandom(seed);//Generate randomNumbers for x
        y=generateRandom(seed);//Generate randomNumbers for y
        //printf("The value of x is %f ",x);
        //printf("The value of y is %f ",y);
        double res = sqrt((x*x)+(y*y));//Finding if the point is inside the circle
        //printf("The value of res is %f ",res);
        if(res<=1)//checking the point inside or outside the circle
            inside++;
    }
    //printf("The value of inside is %d ",inside);
    double probability = (double)(inside / total);//calculating probability
    //printf("The value of probability is %f ",probability);
    double PI = 4 * probability;
    return PI;//Value of PI
}
int main()
{
   srand(time(NULL));
   int seed = 0;
   int result = 0;
   while(result==0){
    printf("Enter the seed for the random number generator: ");
    seed=input(&result);
   }

   int iterations = -1;
   while(iterations<0){
    printf("Enter the number of iterations to run: ");
    iterations=input(&result);
   }

   printf("The value of pi is %.3f.",calculatePI(seed,iterations));
   return 0;
}


/*
Sample Input/Output:
Example 1

Enter the seed for the random number generator: 4
Enter the number of iterations to run: 10
The value of pi is 3.200.

Example 2

Enter the seed for the random number generator: -7
Enter the number of iterations to run: 2
The value of pi is 4.000.

Example 3

Enter the seed for the random number generator: -7
Enter the number of iterations to run: 10000
The value of pi is 3.144.

Example 4

Enter the seed for the random number generator: 3.45
Enter the seed for the random number generator: why
Enter the seed for the random number generator: 12
Enter the number of iterations to run: cat
Enter the number of iterations to run: -1
Enter the number of iterations to run: 3.141
Enter the number of iterations to run: 75
The value of pi is 3.147.
*/

***********************************************************************************************************

The working of each function has been given in the description, kindly look into the code. Each method has been described with its working. If you have any queries, kindly comment.

Kindly rate the answer if you find it useful. All The Best. :)

***********************************************************************************************************************