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.
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!!
Submit a c++ file: 2. Write a program that defines the named constant PI, const double...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of p. The program should use PI and the functions listed in Table 6-1 to accomplish the following: a. Output the value of Pi Output the value of Pi. b. Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs the following: i.   The...
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at...
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at a rate of 1 Hz and the other at a rate of 2 HZ.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT