# solutions16 - // prompt user for number of darts to throw...

This preview shows page 1. Sign up to view the full content.

CSCI-1100 Computer Science I Rensselaer Polytechnic Institute Spring 2004 Studio 16 -- Solutions #include <iostream> #include "Dice.h" using namespace std; int main () //PURPOSE: use Monte Carlo Method to estimate pi //PRECONDITIONS: none //POSTCONDITIONS: displays the estimated value of pi { long totaldarts; // total number of darts thrown long inside; // number of darts inside unit circle float x,y; // coordinates of point where dart lands long countdarts; // count darts thrown (loop variable) float pi; // estimate for pi Dice dart(101); // create die to simulate dart
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // prompt user for number of darts to throw cout &lt;&lt; &quot;Enter number of darts to throw: &quot;; cin &gt;&gt; totaldarts; // loop to throw and count darts inside = 0; for (countdarts=1; countdarts&lt;=totaldarts; countdarts++) { x = (float)(dart.Roll() - 1) / 100.0; y = (float)(dart.Roll() - 1) / 100.0; if ((x*x + y*y) &lt;= 1.0) { inside = inside + 1; } } // compute estimate of pi pi = 4.0 * ((float)inside / (float)totaldarts); // display estimate cout &lt;&lt; &quot;The estimate of pi is: &quot; &lt;&lt; pi &lt;&lt; endl; return 0; }...
View Full Document

## This homework help was uploaded on 04/16/2008 for the course CSCI 1100 taught by Professor Hardwick during the Fall '08 term at Rensselaer Polytechnic Institute.

Ask a homework question - tutors are online