hi i have this assignment in which i am lost ... if someone could help me it would be great .... i dun need someone to write the program ... just some hints ... i will try to compile it myself ...

just let me know how would u do it


problem #1
Program the following simulation: Darts are thrown at random points onto a square with corners
(1, 1) and (−1, −1). If the dart lands inside the unit circle (that is, the circle with center (0, 0) and
radius 1), it is a hit. Otherwise it is a miss. Run this simulation and use it to determine an
approximate value for PI. Explain why this is a better method for estimating than the Buffon
needle program.

problem#2
Write a program that computes the approximate sine of an angle (θ) by using the following
infinite series:
Approximate sine of θ = θ – (θ3/3!) + (θ5/5!) - (θ7/7!) + (θ9/9!) … (other terms follow)
Where θ is in radians and the factorial computations are given as
3! = 3*2*1
5! = 5*4*3*2*1
7! = 7*6*5*4*3*2*1
9! = 9*8*7*6*5*4*3*2*1

N! = N*(N-1)*(N-2)…..*1
Of course, you can’t compute an infinite sum. Just keep adding values until an individual
summation term is less that a certain threshold.
For example, the equation below:
Approximate sine of θ = θ – (θ3/3!) + (θ5/5!) - (θ7/7!) + (θ9/9!)
computes the sin(θ) to 5 terms.
Also, use the C++ library function to compute the sine of theta (sin(θ)) and compute the Percent
Error between the approximate value of sine computed using the approximation and the sine
library function computation. The Percent Error formula is given below:
%error = ((1.0 – sin(θ))/approximate_sine_ θ)*100
Where sin(θ) is the C++ library function used for computing sine of an angle.

my qtz for problem 1 is how would u calculate if the hit is inside the cirlce or not ....

i have come up with x and y points but need to see if it hits the circle or not ..

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

using namespace std;

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

double rand_x(int a, int b)
{
    return a +(b-a)*rand()*(1.0/RAND_MAX);
}


int main()
{
    rand_seed();
    for (int i=1; i<=5;i++)
    {
        double x1=rand_x(-1,1);
        double y1=rand_x(-1,1);
        cout<<" x value "<<x1<<" y value is "<<y1<<"\n" ;
    }
    return 0;
}

the above is for creating random numbers for x and y


dun knw if i am doing it rite or wrong .... just started off with smthin

You need to calculate the distance from (x1,y1) to (0,0). If < 1.0, you have a hit.

And please use English, as the Board Rules request. This is not a chat room.

I'd love to help u wit the first question..but im gettin 1.somethin for Pi!

Member Avatar for iamthwee

Looks like a spin off from the monte carlo method for estimating Pi.

use the equation for a circle;

[tex] x^2 + y^2 = r^2 [/tex]

You may need to shift that so it is on the positive x and y axis to make more sense of the numbers? Don't know though.

You don't need PI nor circles, just the distance formula, as SOS mentioned in your duplicate post.

Member Avatar for iamthwee

>You don't need PI
I never said you needed PI, but the purpose of his assignment is to estimate the value of Pi, hence why I said it was a 'monte carlo estimation of Pi clone'. Google it.

>nor circles
Well the equation which describes the function of a circle where the centre is located at (0,0) is based directly on pythag's eqn (or as you put it the distance formula). So yeah it's basically the same thing.

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.