Program to calculate pie, C/C++ Programming

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of π using a simulation method called "Monte Carlo". The following figure shows a circle of radius 1, centered at the origin and circumscribed by a square.

535_program.png

Imagine that this is a dartboard and that you are tossing darts at it randomly. With enough darts, the ratio of darts in the circle (Nc) to total darts thrown (Nt) is approximately equal the ratio between the area of the circle (circle_area = π(1)2) and the area of the square (square_area = 4) i.e.

2454_equation.png

Therefore, 4=NC/NT . We can simplify the math by only considering the first quadrant, calculating the ratio of the number of darts landed on the first quadrant over the number of darts landed on the top right square's area.

Use the Euclidean distance formula, to check if a dart is landed inside the first quadrant. Maintain account of the number of darts that end up in the first quadrant of the unit circle.

Now use your loop to build a π-calculating function. The function should take one argument specifying the number of dart throws to run. It should return the decimal value of π, using the technique outlined above. You should get pretty good results for around 5,000,000 dart throws! Isn't this neat?

Posted Date: 2/15/2013 7:40:27 AM | Location : United States







Related Discussions:- Program to calculate pie, Assignment Help, Ask Question on Program to calculate pie, Get Answer, Expert's Help, Program to calculate pie Discussions

Write discussion on Program to calculate pie
Your posts are moderated
Related Questions
What is the difference among a copy constructor and an overloaded assignment operator? Ans) A copy constructor constructs a latest object by using the content of the argument

count the number of string in n-th padovan string

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

find the area of a curve y=f(x) between x=a and x=b integrate f(x) between limits a and b   #include float start_point, /* GLOBAL VARIABLES */

Assignment:  write a C program and a MASM procedure.  The C program calls the MASM procedure to perform letter case conversion. Text sections covered:  12.1 to 12.3.1 Write a

Intelligent homes are becoming increasingly popular as the cost/performance ratio of microcontrollers is continuously dropping.  These systems incorporate various transducers to de

A: No.       While realloc() need to copy the allocation, this uses a bitwise copy operation, that will tear several C++ objects to shreds. C++ objects must be allowed to copy t

Many modern cryptography algorithms require two numbers that are coprime, or sometimes referred to as relatively prime. Two numbers are coprime if their greatest common divisor is

how to make a game

(a) Write a Scheme procedure to evaluate the expression: 7/6+2*5+(3*2+6*7)*4. (b) Write a Scheme procedure to evaluate the expression: 2*(-1+(-3+4*2-7)*3/(3*2)).