#include <stdlib.h>
#include <iostream.h>
#include <math.h>


long GetAmmunition(void)
{
        long    munition;
        long    seed;
        cout << "Please Professor, how many shots should we fire?";
        // Read shots, maybe we should use that GetIntegerInRange
        // to make certain that the prof enters a sensible value
        // Risk it, hope he can type a number
        cin >> munition;
        // Need seed for random number generator,
        // Get prof to enter it (that way he has chance of
        // getting same results twice).  Don't confuse him
        // with random number stuff, just get a number.
        cout << "Please Professor, which gunner should lay the gun?"
                        << endl;
        cout << "Please enter a gunner identifier number 1..1000 :";
        cin >> seed;
        srand(seed);
        return munition;
}

int TestInCircle(double x, double y)
{
        // Use the sqrt() function from math.h 
        // rather than NewtRoot()!
        // Remember to #include <math.h>
        double distance = sqrt(x*x + y*y);
        return distance < 100.0;
}

double PiEstimate(long squareshot, long circleshot)
{
        return 4.0*double(circleshot)/squareshot;
}


double GetCoordinate(void)
{
        int     n = rand();
        // Take n modulo 201, i.e. remainder on dividing by 201
        // That should be a number in range 0 to 200
        // deduct 100
        n = n % 201;
        return double(n-100);
}


void    PrintPIEstimate(double pi, long shotsused)
{
        cout << "Estimate for pi, based on results of"
          << shotsused << " shots, is " << pi << endl;
}

int main()
{
        long    fired = 0, ontarget = 0, ammunition;
        double  pi = 4.0; // Use the colonel's estimate!
        long    freq, count;
        
        ammunition = GetAmmunition();

        freq = ammunition / 10;
        count = 0;

        for(; ammunition > 0; ammunition--) {
                double x, y;
                fired++;
                x = GetCoordinate();
                y = GetCoordinate();
                if(TestInCircle(x, y))
                        ontarget++;
                
                count++;
                if(count == freq) {
                        // Time for another estimate
                        pi = PiEstimate(fired, ontarget);
                        PrintPIEstimate(pi, fired);
                        // reset count to start over
                        count = 0;
                        }
                }

        cout << "We've used all the ammunition." << endl;
        cout << "Our final result:" << endl;
        pi = PiEstimate(fired, ontarget);
        PrintPIEstimate(pi, fired);
        return 0;
}


