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
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:
Well, here is a hint...
4 * atan(1)
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.
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
Search google for "compute pi" , plenty of stuff comes up.
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!
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.
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
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.
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
thereforeCode:circumference = 2 * PI * radius;
However, if you wanted to compute pi using this ratio you'd be screwed because to accurately compute the circumference of a circle you need to know pi. You're much better off (from a programming point of view) using Imperito's solution ofCode:PI = circumference / (2 * radius);
// or
PI = circumference / diameter;
and just use that computed value of pi for all of your calculations.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:
As you can see, the hypotenuse is the longest side and is across the triangle from the right angle, while the opposite is across from the angle you are interested in, and the adjacent is the third side. The tangent function can be defined as the ratio of the lengths of the opposite and the adjacent. Now, a 360 degree angle can also be referred to as 2*pi radians (take my word for it if you aren't familiar with radians, they're another way to talk about angles and I don't have the time to get into it here.) You can't have a right triangle with a 360-degree angle in it, but you can have a right triangle with a 45-degree angle in it (45 = 360 / 8). Since the angles have to add up to 180, that means that the two non-right angles must both be 45 degrees, which means that the opposite and the adjacent angles must be equal, so the tangent of 45 degrees is the ratio of two equal quantities, which is 1.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.
oops, nm
*edited out*
Nice post Geophyzzer!!! I'm going to print that out and study it when I have a few minuites to absorb it.
Don't forget to:Quote:
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
thereforeCode:circumference = 2 * PI * radius;
However, if you wanted to compute pi using this ratio you'd be screwed because to accurately compute the circumference of a circle you need to know pi. You're much better off (from a programming point of view) using Imperito's solution ofCode:PI = circumference / (2 * radius);
// or
PI = circumference / diameter;
and just use that computed value of pi for all of your calculations.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:
As you can see, the hypotenuse is the longest side and is across the triangle from the right angle, while the opposite is across from the angle you are interested in, and the adjacent is the third side. The tangent function can be defined as the ratio of the lengths of the opposite and the adjacent. Now, a 360 degree angle can also be referred to as 2*pi radians (take my word for it if you aren't familiar with radians, they're another way to talk about angles and I don't have the time to get into it here.) You can't have a right triangle with a 360-degree angle in it, but you can have a right triangle with a 45-degree angle in it (45 = 360 / 8). Since the angles have to add up to 180, that means that the two non-right angles must both be 45 degrees, which means that the opposite and the adjacent angles must be equal, so the tangent of 45 degrees is the ratio of two equal quantities, which is 1.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;)
Or there's always cout >> "3.14159" - I think you get the point.
Isn't this the correct way, sean?Quote:
Originally posted by Sean
Or there's always cout >> "3.14159" - I think you get the po
int.
cout << "3.14159";
;)