Below is the start of my source code for a program I am trying to figure out. We have to compute the approximate value of PI with random throws at a dartboard. It is considered a hit if it lands inside unit circle (0,0) and radius 1. Any help on understanding this problem? I am still in the design phase.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const double PI = 3.141592653589793;
static double x_cor = 0;
static double y_cor = 0;
static double x_sum = 0;
static double y_sum = 0;
double x_y_total = 0;
long int num_darts = 0;
long int num_hits = 0;
long int num_misses = 0;
long double pi_approx = 0;
srand(time(0));
while ( num_darts != 0 ) // Start of while loop
{
cout << " How many times do you want to throw the darts: ";
cin >> num_darts;
cout << endl;
if ( num_darts != 0 )
{
for ( int i = 0; i < num_darts; i++) // Start of for loopa inside the while loop
{
x_cor = static_cast<double>(rand())/RAND_MAX;
y_cor = static_cast<double>(rand())/RAND_MAX;
x_sum = (x_cor * x_cor);
y_sum = (y_cor * y_cor);
x_y_total = x_sum + y_sum;
if (x_y_total < 1)
{
num_hits++;
}
else
{
num_misses++;
}
}
} // end of if loop
pi_approx = (num_hits / num_darts);
} // end of while loop
cout << " The value is " << pi_approx << endl;
return 0;
}