Program to calculate pie, C/C++ Programming

Assignment Help:

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?


Related Discussions:- Program to calculate pie

Amie, what happens when the following command is used? chmod u=rwx,go=r-x f...

what happens when the following command is used? chmod u=rwx,go=r-x foo

Define the conditional operator in c language, Define the Conditional Opera...

Define the Conditional Operator in c language? The Simple conditional operator can be carried out with the conditional operators (? And :). An expression that makes use of the

Minimum Shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Ascii string related, A string S is said to be "Super ASCII", if it contain...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Explain public and private members, Public, Private and Protected members: ...

Public, Private and Protected members: Class members can either be declared in public','protected' or in the 'private' sections of the class. But as one of the features of OOP i

Working of ordered linked list, Working Ordered linked list: • Eachint...

Working Ordered linked list: • Eachinteger in the queue is stored inside of a QueueItem. The QueueItem contains the integer, and a pointer to the next item in the queue. Fo

First line starts with T, Byteland county is very famous for luminous jewe...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

The Shell or Command Line Interpreter is the fundamental Use, 1. The shell ...

1. The shell must support the following internal commands: i. cd - Change the current default directory to . If the argument is not present, report the current directory. If t

Create program to prints out the value of a variable, Here is a short progr...

Here is a short program. It prints out the value of a variable "x". Ernie and Bert disagree about what will be printed: Ernie says, the value gets changed in "changeX" so it will p

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd