Question

In: Computer Science

Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is...

Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is NOT allowed.

Solutions

Expert Solution

// C code to approximate pi

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
/* initialize random numbers */
srand(time(NULL));

int iterations=0, index, count = 0;
double xcoordinate,ycoordinate;
double square_radius;
double approx_pi;

printf("Enter the number of iterations: ");
scanf("%d",&iterations);

for ( index=0; index<iterations; index++)
{
xcoordinate = (double)rand()/RAND_MAX;
ycoordinate = (double)rand()/RAND_MAX;
  
square_radius = xcoordinate*xcoordinate+ycoordinate*ycoordinate;
  
if (square_radius<=1)
count++;
}

approx_pi=(double)count/iterations*4;

printf("Approximate value pf pi: %f\n",approx_pi);

return 0;
}

/*
output:

Enter the number of iterations: 100
Approximate value pf pi: 3.240000

Enter the number of iterations: 10000
Approximate value pf pi: 3.142800

Enter the number of iterations: 100000000
Approximate value pf pi: 3.141976


*/


Related Solutions

Write a matlab program that determines the value of pi using the monte carlo technique. do...
Write a matlab program that determines the value of pi using the monte carlo technique. do this for a loop of multiple fixed points. (i.e 100-10000) Plot the computed value of pi and the difference from the true value as this number increases. Time the execution of your code for various numbers of points, and plot the precision vs the computational cost.
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
How to do the monte carlo method on a calculator? step by step method.
How to do the monte carlo method on a calculator? step by step method.
A Monte Carlo simulation is a method for finding a value that is difficult to compute...
A Monte Carlo simulation is a method for finding a value that is difficult to compute by performing many random experiments. For example, suppose we wanted to estimate π to within a certain accuracy. We could do so by randomly (and independently) sampling n points from the unit square and counting how many of them are inside the unit circle (assuming that the probability of selecting a point in a given region is proportional to the area of the region)....
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
1. Compare paramatric, historical, and monte carlo simulation methods in identifying VaR (value at risk) 2....
1. Compare paramatric, historical, and monte carlo simulation methods in identifying VaR (value at risk) 2. What are the pros and cons of those? 3. Identify some weights on historical losses that you think should make more sense in current trading war environment.
Why the Monte Carlo Method is so important today Article ID Dirk P. Kroese The University...
Why the Monte Carlo Method is so important today Article ID Dirk P. Kroese The University of Queensland Tim Brereton Ulm University Thomas Taimre The University of Queensland Zdravko I. Botev The University of New South Wales Keywords: Monte Carlo method, simulation, MCMC, estimation, randomized optimization Abstract: Since the beginning of electronic computing, people have been interested in carrying out random experiments on a computer. Such Monte Carlo techniques are now an essential ingredient in many quantitative investigations. Why is...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT