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

Write a program in C to estimate the area of a circle using monte carlo method.

ID: 3563852 • Letter: W

Question

Write a program in C to estimate the area of a circle using monte carlo method. Consider a circle and a square which bounds the circle. By generating random points in the square and checking if they are in the circle you can estimate the area of the circle. The following equality should hold for random points. You can compute the left-hand side by just using the random points. You can compute the area of square using the formula and estimate the area of circle:

# of random points in a circle = area of a circle

# of random points in a square = area of a square

Read the radius and seed of random number generator and display the estimate for the area of circle in increments of 100.  

use 3.141592 for PI
use srand() to set the seed of the random number generator
use the formula to get exact area of circle to see how close the area estimate is

Sample output:

Enter radius and random number seed

100 3

iterations estimate

100 31200.000000

200 32200.000000

300 31466.666667

(and so on..)

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int radius;
int seed;
int count = 0;
int k = 0,j;
float area = 0;
int x,y;
printf("Enter radius and random number seed :");
scanf("%d %d",&radius,&seed);
srand(seed);
printf(" iterations estimate");
for(k=100; k<=900; k+=100){
count = 0;
for(j = 0; j<=k; j++){
    x = rand()%(2*radius+1)-radius;
   y = rand()%(2*radius+1)-radius;
   if(x*x+y*y <= radius*radius)
   count++;
}
area =4* (float)(radius*radius)*((float)count/k);
printf(" %d    %.6f",k,area);
}
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