![]() |
|
#1
|
|||
|
|||
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:
CPP / C++ / C Code:
Driver CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Basic C++ Help (arrays, pointers, rand)Quote:
Here's one approach:
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:
A few runs Code:
Code:
Code:
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
|
|||
|
|||
Re: Design a program that will compute pi by...Edit: Found my mistake.
Thanks for the help |
|
#4
|
|||
|
|||
Re: Design a program that will compute pi by...CPP / C++ / C Code:
CPP / C++ / C Code:
|
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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