GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Mar-2009, 15:02
idknox idknox is offline
New Member
 
Join Date: Mar 2009
Posts: 2
idknox is on a distinguished road

Design a program that will compute pi by...


I'm pretty new to c++....bear with me.

I'm trying to design a program that will compute pi by the following method:

If we draw a quarter circle of unit radius inside a unit square, then the ratio of the area of the quarter circle to the square is ( pi(1^2)/4) / 1^2 = pi/4. Now, generate a set of points inside and on the unit square which inscribes the quarter circle of unit radius. Then the ratio of the number of points inside and on the quarter circle to the number of points inside and on the unit square approximates pi/4.

What I have compiles but spits out an entirely incorrect answer.

This is what I have, thanks in advance for any help.

Prototypes
CPP / C++ / C Code:
double arrayx(double*, const int);
double arrayy(double*, const int);
double sqrt(double, double*, double*);
double test(double);
CPP / C++ / C Code:
                                                   

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include "pa4functions.h"

double arrayx(double* x, const int dx)
{
        for(int i=0; i<dx; ++i)
        {
                std::srand(time(0));

                x[i] = (double)rand()/(double)RAND_MAX;
        }
}

double arrayy(double* y, const int dy)
{
        for(int i=0; i<dy; ++i)
        {
                std::srand(time(0));

                y[i] = (double)rand()/(double)RAND_MAX;
        }
}

double sqrt(double, double* x, double* y)
{
        std::srand(time(0));
        double a = *(x+rand()%100000+1);
        double b = *(y+rand()%100000+1);         
        return sqrt(a*a+b*b);
}

double test(double r)
{
        int c;

        for(int i=0; i<c; ++i)
        {
                if(r<=1)
                {
                        ++c;

                }
        }

        double result = (c/100000)*4;

        std::cout << "Pi is " << result << '\n';
}


Driver
CPP / C++ / C Code:
#include "pa4functions.h"

int main()
{
        const int dx = 100000;
        const int dy = 100000;
        double x[dx];
        double y[dy];

        arrayx(x, dx);

        arrayy(y, dy);

        double r = sqrt(r, x, y);

        test(r);

        return 0;
}
  #2  
Old 03-Mar-2009, 16:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Basic C++ Help (arrays, pointers, rand)


Quote:
Originally Posted by idknox
I'm trying to design a program that will compute pi ...

Here's one approach:
  1. Generate a bunch of pairs of real numbers (x,y) where 0 <= x <= 1 and 0 <= y <= 1
    You generated 100000 pairs and stored the values in arrays of doubles. That could work if you did the following correctly.

  2. Set a counter to zero.

  3. Make a loop that does the following:
    Look at each and every point. Increment the counter if (and only if) the point is inside the unit circle.

  4. After the loop terminates, print out the value of the counter divided by the number of points. We want to see how close this value is to pi/4

Here are a few problems that I see:

1. What is your function "sqrt" supposed to do? It looks to me as if it returns the value associated with a single (random) point from your arrays. Why? What's the point?

2. Your function "test" goes through some kind of loop. It has a single argument "r" and you don't initialize the variable "c" so there is no telling how many times it goes through the loop.

3. Since the value of "r" doesn't change, then either "c" is incremented each and every time through the loop, or it is never incremented.
That's kind of irrelevant, since you didn't give it a value to start with. (Some compilers might initialize it to zero and some will not. It's undefined.)

4. Finally, you divide an integer by an integer and get another integer. Integer division could never give a reasonable approximation to pi/4, even if you were dividing the correct numbers in the first place.

Here's my suggestion:

1. If you want to use arrays, you may find out that compilers limit the amount of storage to something like a megabyte or so. (Some have more capability, but I wouldn't count on it). I would try it with array sized of 10000 for starters, then try bigger. Even 1000 points should give you reasonable start to an approximation to pi/4: somewhere between .77 and .80 I think.

2. Do not call srand() more than once in the entire program. Once the random number has been seeded, just call rand() as many times as you want to. Re-seeding with the same number just makes the sequence repeat, and that's not what you want to do.

3. Unless your assignment was specifically to use arrays, why not just generate the points and test them on the fly?

The guts of the main program could look like:
CPP / C++ / C Code:
 
    const unsigned num_points = 1000; // Or whatever you want to make it
    srand(time(0));
    int sum = 0;
    for (unsigned i = 0; i < num_points; i++) {
        if (is_interior(rand1(),rand1())) {
            ++sum;
        }
    }
    cout << "Total number of points = " << num_points << endl;;
    cout << "Number of interior points = " << sum << endl;
    double pio4 = double(sum)/double(num_points);
    cout << fixed << setprecision(6);
    cout << "Pi/4 approximation = " << pio4 << endl;
    cout << "Pi   approximation = " << pio4*4.0 << endl;

A few runs
Code:
Total number of points = 1000 Points inside the unit circle = 771 Pi/4 approximation = 0.771000 Pi approximation = 3.084000
Code:
Total number of points = 1000 Points inside the unit circle = 798 Pi/4 approximation = 0.798000 Pi approximation = 3.192000
Code:
Total number of points = 100000000 Points inside the unit circle = 78534498 Pi/4 approximation = 0.785345 Pi approximation = 3.141380
My function ran1() is like what you used in your array generators: It returns a double precision value z where 0 <= z <= 1

My function is_interior() returns a value of true if the point (x,y) is inside or on the unit circle.

If you want to use arrays, just fill the arrays as you do now, then make your test function loop through the arrays, testing each point as you go. Increment the counter at each interior point.

I hate to repeat myself, but I suggest that you get it working with arrays of a thousand or so points and then try larger and larger ones.

Regards,

Dave
  #3  
Old 10-Mar-2009, 15:10
idknox idknox is offline
New Member
 
Join Date: Mar 2009
Posts: 2
idknox is on a distinguished road
Arrow

Re: Design a program that will compute pi by...


Edit: Found my mistake.

Thanks for the help
  #4  
Old 10-Mar-2009, 15:29
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: Design a program that will compute pi by...


CPP / C++ / C Code:
return (c/100000)*4;
Integer division. Do this in C++:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    cout << 1/4 << endl;
    cout << 1.0/4 << endl;
    cout << 5/4 << endl;
    cout << 5.0/4 << endl;
}
Basically, if you divide an integer by an integer, you get integer division. If you remember your long division, imagine doing division that way and throwing out any remainder. As long as you divide a floating point number (something with a decimal) by an integer, or an integer by a floating point number, you'll get a floating point answer.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers, Functions, arrays allican57@yahoo C++ Forum 12 10-Nov-2006 15:17
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35
Using Indexes w/ Pointers to Arrays Instead of Pointer Arithmetic? BobbyMurcerFan C++ Forum 6 20-Dec-2004 12:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.