Determining Experimentally: Recall that pi is the ratio of a circle\'s circumfer
ID: 3785703 • Letter: D
Question
Determining Experimentally: Recall that pi is the ratio of a circle's circumference to its diameter and that we can calculate the area of a circle with the formula A = pi r^2. Below is a circle enscribed within the unit Square. What is the ratio of the areas of the enscribed circle to that of the unit square? If we pick a random point within the unit square what is the probability that the point will also lie within the circle? If we repeat this experiment an arbitrarily large number of times the ratio of the number of points which lie within the circle to the number of points within the unit square (all of them) will approach pi/4. Using the language structures we have discussed write a program that will do the above experiment an arbitary (determined at run-time) number of times and report back the approximate value of pi.Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main( ) {
int n, c= 0;
cout << "Please enter number of iterations to use in approximating PI? ";
cin >> n;
//For generating random number
srand( time( NULL ) );
for ( int i= 0; i < n; ++i ) {
// RAND_MAX is the maximum value returned by rand
//Calculating roots
double x= 2.0 * (rand() / (1.0 + RAND_MAX)) - 1.0;
double y= 2.0 * (rand() / (1.0 + RAND_MAX)) - 1.0;
if ( sqrt( x*x + y*y ) <= 1.0 )
++c;
}
printf( "%lg ", 4.0 * c / (double) n );
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.