hey i need help i have just started my programming class and need to make a program that calculates pi. can anyone help me?

it would be real nice.

:rolleyes:

Printable View

- 11-12-2002loki_cmrPi
hey i need help i have just started my programming class and need to make a program that calculates pi. can anyone help me?

it would be real nice.

:rolleyes: - 11-12-2002Imperito
Well, here is a hint...

4 * atan(1) - 11-12-2002Eibro
Google returned this webpage: http://www.cygnus-software.com/misc/pidigits.htm Try doing a search. Write some code, post any problems you're having back here and people will be happy to help you.

- 11-12-2002kermi3
Hi loki

First of all I'm sorry I havn'e greeted you yet, I'm trying to do that...so welcome heh.

Secondly, what are you having problems with? Is it the algorithm that you're not getting? Generally people aren't going to write it all for you, but they're very happy to help you understand and find errors. What have you got so far? Where are you? Is it your code where's there's trouble?

Let us know....

Good Luck,

Kermi3 - 11-12-2002beege31337
Search google for "compute pi" , plenty of stuff comes up.

- 11-12-2002loki_cmr
thanks all but the thing is i think im going for something less complicated than what u guys are think. i read the web page and it has a lot of stuff i dont understand like tan and cos. the reason i dont undertand that is because im only a ninth grader and im in geometry not trigonometry. anyway what i want to know is if its true that pi is a ratio of circumference and something else, and if it is what are the two thing and is their a way i can generalize them. so that i dont have to write to specific things to make the ratio. another problem i was having was finding a way to display all the numbers on the screen.

one more thing kinda off the subject, are there any free compilers that i can get off teh net that are kinda like borlands turbo c++ that use console and not DOS. i think thats what i mean, im not to computer savy,i think thats how u spell it.

:D thanks for all the help so far! - 11-12-2002Imperito
Like I said, pi is 4 times atan(1). Now the atan of 1 is tricky to compute, which is where all the algos come in. But the math library of you compiler can do that, too, just not to amazing precision.

Just #include <cmath> at the top, and then cout << 4*atan((long double)1). By casting the 1 to a long double you ensure that the call is resolved to the highest precision the library function supports. - 11-12-2002UniqueScreenNam
Carlos, I got the algorithm. I'll show it to you tommorow, it's really simple. Also, I figured out how to implement it, so no worries. It's late anyway, nother 30 minutes won't matter.

Everyone go home, nothing to post here :D - 11-13-2002master5001
I fully understand the desire to do something superceding your desire to learn a concept necessary in order to achieve the desired result. However, tangents and cosines are fairly basic mathematical concepts. You are going to need to take a few more math classes. I know that isn't what you want to hear but that is the unfortunate truth.

- 11-13-2002geophyzzer
loki, if you want a direct answer to your question, pi can be expressed as the ratio of the circumference of a circle to its diameter; this comes from rearranging the well-known formula

Code:`circumference = 2 * PI * radius;`

Code:`PI = circumference / (2 * radius);`

// or

PI = circumference / diameter;

Code:`const double pi = 4.0 * atan(1.0);`

If you need a mathematical definition for why this works, here goes (I'll try to keep in mind that you haven't had trig yet). If nothing else this will help you when you do get to trig:

Consider a right triangle (one angle is 90 degrees, the others are arbitrary but must add up to 90 degrees themselves). The tangent of an angle can be defined using this triangle; set one of the other angles equal to the angle that you are trying to calculate, the other to (90 - angle). The three sides of the triangle can be referred to as the adjacent, the opposite, and the hypotenuse:

Code:

|\

| \

| \ hypotenuse

opposite | \

| \

|_____\<-- angle of interest

adjacent

Still with me? Good. Now, we have the expression tangent(45 degrees) = 1. Since 360 degrees = 2*pi, 45 degrees = pi / 4. We now have tangent(pi / 4) = 1. Here is where the atan function comes in; atan(x) returns "the angle for which the tangent is x". Since we are interested in the angle pi / 4, we take atan(1), and to get pi we multiply by 4. Thus, pi = 4 * atan(1).

A bit long-winded, but I think it will help you considerably when you start studying these things in school. - 11-13-2002...
oops, nm

*edited out* - 11-13-2002DougDbugImpressive, Geophyzzer!
Nice post Geophyzzer!!! I'm going to print that out and study it when I have a few minuites to absorb it.

- 11-13-2002correlcjQuote:

*Originally posted by geophyzzer*

**loki, if you want a direct answer to your question, pi can be expressed as the ratio of the circumference of a circle to its diameter; this comes from rearranging the well-known formula**

Code:`circumference = 2 * PI * radius;`

Code:`PI = circumference / (2 * radius);`

// or

PI = circumference / diameter;

Code:`const double pi = 4.0 * atan(1.0);`

If you need a mathematical definition for why this works, here goes (I'll try to keep in mind that you haven't had trig yet). If nothing else this will help you when you do get to trig:

Consider a right triangle (one angle is 90 degrees, the others are arbitrary but must add up to 90 degrees themselves). The tangent of an angle can be defined using this triangle; set one of the other angles equal to the angle that you are trying to calculate, the other to (90 - angle). The three sides of the triangle can be referred to as the adjacent, the opposite, and the hypotenuse:

Code:

|\

| \

| \ hypotenuse

opposite | \

| \

|_____\<-- angle of interest

adjacent

Still with me? Good. Now, we have the expression tangent(45 degrees) = 1. Since 360 degrees = 2*pi, 45 degrees = pi / 4. We now have tangent(pi / 4) = 1. Here is where the atan function comes in; atan(x) returns "the angle for which the tangent is x". Since we are interested in the angle pi / 4, we take atan(1), and to get pi we multiply by 4. Thus, pi = 4 * atan(1).

A bit long-winded, but I think it will help you considerably when you start studying these things in school.

#define PI 3.14159

//place this just below your #inlcude<iostream>

// another hint;) - 11-13-2002sean
Or there's always cout >> "3.14159" - I think you get the point.

- 11-13-2002correlcjSorry sean but isn't it like this...Quote:

*Originally posted by Sean*

**Or there's always cout >> "3.14159" - I think you get the po**

int.

cout << "3.14159";

;)