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;
}

Here's how to understand the problem:

Run through your code, step by step, on paper. What did you want to happen? Why is it not happening?

Right now, it looks like your program is trying to calculate the value of pi/4.

Will ( num_darts != 0 ) evaluate to true the first time?

I have modified my source code for this problem but I am having an error inside of my getRandomNum function and I can't figure it out. Any ideas?

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>


using namespace std;
void rand_seed();
double get_RandomNum(double,double);

int main()
{




        const double PI = 3.141592653589793;
        double x_cor = 0;
        double y_cor = 0;
        double x_sum = 0;
        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;

        rand_seed();

        double x_y_total = 0;
        long int num_darts = 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;

        rand_seed();



        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 loop inside the while loop
                {



                        x_cor =get_RandomNum(1.0,-1.0)/RAND_MAX;
                        y_cor =get_RandomNum(-1.0,-1.0)/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_misses);
        } // end of while loop

                cout << " The value is  " << pi_approx << endl;
        return 0;
}


        void rand_seed()
        {
                int seed = static_cast<int>(time(0));
                srand(seed);
        }

        double get_RandomNum(double x, double y)
        {
                double max = x + static_cast<double> rand() % (y-x+1);
//              return x + rand() % (y - x + 1);
                return max;
        }

[hs001@cs hs001]$ g++ p7-14.cpp
p7-14.cpp: In function `double get_RandomNum(double, double)':
p7-14.cpp:89: syntax error before `(' token
[hs001@cs hs001]$ g++ p7-14.cpp

I have modified the program again but my values seem a little high.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>


void rand_seed();
double get_RandomNum(double,double);

using namespace std;


int main()
{

    rand_seed();

    double x_cor = 0;
    double y_cor = 0;
    double x_sum = 0;
    double y_sum = 0;
    double x_y_total = 0;
    int num_darts = 0;
    int num_hits = 0;
    int num_misses = 0;
    double pi_approx = 0;




    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 loop inside the while loop
        {



            x_cor =get_RandomNum(1.0,-1.0);
            y_cor =get_RandomNum(-1.0,1.0);

            x_sum = (x_cor * x_cor); y_sum = (y_cor * y_cor);

            x_y_total = sqrt(x_sum + y_sum);

                if (x_y_total <= 1)
                {
                    num_hits++;
                }
                else
                {
                    num_misses++;
                }

        }

    }  // end of if loop    

    pi_approx = (1.0 * num_hits  /num_misses);

        cout << " The value is  " << pi_approx << endl;   
    return 0;
}


    void rand_seed()
    {
        int seed = static_cast<int>(time(0));
        srand(seed);
    }

    double get_RandomNum(double x, double y)
    {
        return x + (y - x) *  rand() * (1.0 / RAND_MAX);
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.