Please, using C language, not C+or C++. Thanks. Files to submit: pi.c Time it to
ID: 3603497 • Letter: P
Question
Please, using C language, not C+or C++. Thanks.
Files to submit: pi.c
Time it took Matthew to Complete: 20 mins
Requriements
Program must compile with both -Wall and -Werror options enabled
Submit only the files requested
Use doubles to store real numbers
Print all doubles to 2 decimal points unless stated otherwise
Restrictions
No global variables may be used
Your main function may only declare variables and call other functions
Description
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
In order to match the outputs on Mimir 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
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.
Show transcribed image text
(0,0) (0-1)Explanation / Answer
#include <stdio.h> /*printf() scanf()*/
#include <ctype.h> /*For isspace()*/
#include <stdlib.h> /*For srand() rand()*/
int main(int argc, char const *argv[])
{
/*Variable to store number of iterations*/
unsigned int number_iterations;
/*character in the input buffer to reach the newline character*/
char terminal;
/*seed_value: seed for random number generator
* count: counts number of times point genererated is in circle
* iterator: for loop iterartor
*/
int seed_value, count = 0, iterator = 0;
/*
* x: point x
* y: point y
* z: to check if point is in circle or outside
* circle equation : z = x^2 + y^2, z will be one or less than
* if point (x,y) is in circle
*/
double x, y, z, pi;
/*LO and HIGH to generate random points in range [-1 1]*/
const double LO = -1, HI = 1;
printf("Enter the seed for the random number generator: ");
/*scanf() returns 2 if both completed succesfully and isspace returns
* true if terminal is whitespace character
*/
while(scanf("%d%c", &seed_value, &terminal) != 2 || !isspace(terminal))
{
/*To clear the buffer*/
while ((getchar()) != ' ');
printf("Enter the seed for the random number generator: ");
}
/*If terminal is not newline clear the buffer*/
if(terminal != ' ') while((getchar()) != ' ');
printf("Enter the number of iterations to run: ");
while(scanf("%u%c", &number_iterations, &terminal) != 2 || !isspace(terminal))
{
while ((getchar()) != ' ');
printf("Enter the number of iterations to run: ");
}
/*Seed the random variable*/
srand(seed_value);
/*iterations number_iterations times*/
for (iterator=0; iterator<number_iterations; iterator++) {
/*generate x coordinate value*/
x = LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO));
y = LO + ((double) rand()) / (((double) RAND_MAX) / (HI - LO));
/*Equation of circle*/
z = x*x + y*y;
/*Check if point lies in circle or not*/
if (z<=1) count++;
}
/*probability(point inside circle) * 4*/
pi=((double)count/(double)number_iterations)*4;
/*approximate value of pi*/
printf("The value of pi is %.3f ", pi);
return 0;
}
OUTPUT
=============================================
$ gcc pi.c -Wall -Werror && ./a.out
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: 122
The value of pi is 2.984
$ gcc pi.c -Wall -Werror && ./a.out
Enter the seed for the random number generator: 12
Enter the number of iterations to run: 12
The value of pi is 3.667