Pi Approximation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 7
Reputation: essentialc++33 is an unknown quantity at this point 
Solved Threads: 0
essentialc++33 essentialc++33 is offline Offline
Newbie Poster

Pi Approximation

 
1
  #1
Mar 28th, 2004
The value for PI can be determined by the series equation
PI=4x(1-1/3+1/5-1/7+1/9-1/11+1/13-....)
write an interactive program that asks the user how manu terms of series
equation to use in approximating PI. Then calculate and display the
approximation. Here is a sample run:
PI Approximation Program
How many terms of the series should be included?
(The more terms, the better the approximation)
=> 3
Approximation value of pi is 3.466667.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: programming

 
0
  #2
Mar 28th, 2004
write some code, and then ask us for help when u get stuck. read the sticky about homework.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Pi Approximation

 
0
  #3
Mar 28th, 2004
Originally Posted by essentialc++33
Approximation value of pi is 3.466667.
Don't you mean ~3.1415 ... ??
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Pi Approximation

 
0
  #4
Mar 28th, 2004
I hope he does, 3.4 is a tad bit off .... hey try to do the project on your own and when you get absolutley then specify your exact problem and post some code then we can help you out
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 7
Reputation: essentialc++33 is an unknown quantity at this point 
Solved Threads: 0
essentialc++33 essentialc++33 is offline Offline
Newbie Poster

Re: Pi Approximation

 
0
  #5
Mar 28th, 2004
the book sample shows 3.466667.
this is what I wrote:
#include
<iostream>
#include <cmath>

usingnamespace std;

int main ()

{

double pi =3.14159265358979323846; //approximate value of pi.

chardouble terms;

pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...);

cout << "Please enter how many terms of the series equation to use in approximating pi"<< endl;

cin >> enter terms;

cout << "approximate value of pi is"<< endl;
return 0;
}

I don't know what I am missing or what esle to do.
Last edited by essentialc++33; Mar 28th, 2004 at 3:52 pm. Reason: send the copy for what i wrote
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 7
Reputation: essentialc++33 is an unknown quantity at this point 
Solved Threads: 0
essentialc++33 essentialc++33 is offline Offline
Newbie Poster

Re: programming

 
0
  #6
Mar 28th, 2004
Originally Posted by infamous
write some code, and then ask us for help when u get stuck. read the sI don't know what I am missing or what esle to do.ticky about homework.
This is what I wrote: and I don't know where else to go.
#include <iostream>

#include <cmath>

usingnamespace std;

int main ()

{

double pi =3.14159265358979323846; //approximate value of pi.

chardouble terms;

pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...);

cout << "Please enter how many terms of the series equation to use in approximating pi"<< endl;

cin >> enter terms;

cout << "approximate value of pi is"<< endl;

return 0;

}

Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Pi Approximation

 
0
  #7
Mar 28th, 2004
you need to display pi when it has been approximated:

cout << "approximate value of pi is"<<pi<<endl;
and pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...); will not work becuase the compiler can not calculate the ... so to coninue you would need some sort of looped structure. Then when it has done that, all you have to do is multiply by 4.

heres an example of how i would do it:

#include <iostream.h>
#include <math.h>
int main()
{
	 double pi = 0; // Calculating pi/4
	 int elements = 2000;
	 for (long int n = 1; n <= elements; n++)
	 {
		 pi += (double) pow(-1, n+1)/(2*n-1);
	 } 
// Calculating pi
pi *= 4;
cout << "Estimated PI value (" << elements << " elements): "<< pi;
return 0;
}


As you can see I left some work in there for you, but it up to you to find out how the program works and what you need to modify to allow the user to determin the number of cycles.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 7
Reputation: essentialc++33 is an unknown quantity at this point 
Solved Threads: 0
essentialc++33 essentialc++33 is offline Offline
Newbie Poster

Re: Pi Approximation

 
0
  #8
Mar 28th, 2004
Originally Posted by BountyX
you need to display pi when it has been approximated:

cout << "approximate value of pi is"<<pi<<endl;
and pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...); will not work becuase the compiler can not calculate the ... so to coninue you would need some sort of looped structure. Then when it has done that, all you have to do is multiply by 4.

heres an example of how i would do it:

#include <iostream.h>
#include <math.h>
int main()
{
	 double pi = 0; // Calculating pi/4
	 int elements = 2000;
	 for (long int n = 1; n <= elements; n++)
	 {
		 pi += (double) pow(-1, n+1)/(2*n-1);
	 } 
// Calculating pi
pi *= 4;
cout << "Estimated PI value (" << elements << " elements): "<< pi;
return 0;
}


As you can see I left some work in there for you, but it up to you to find out how the program works and what you need to modify to allow the user to determin the number of cycles.
I modify it the way you told me too and send ask the user how to enter info. but the program tells me that path <iostream.h> does not exist.
do you think i should not use that, i tried <isotream> i still get error.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Pi Approximation

 
-1
  #9
Mar 28th, 2004
usingnamespace std; use that with <iostream> and it should work.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 7
Reputation: essentialc++33 is an unknown quantity at this point 
Solved Threads: 0
essentialc++33 essentialc++33 is offline Offline
Newbie Poster

Re: Pi Approximation

 
0
  #10
Mar 28th, 2004
Originally Posted by BountyX
usingnamespace std; use that with <iostream> and it should work.
when i have usingnamespace std; there already and it is stil not working? it gave me more errors than before.
it says about <math.h> i removed that too.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 20410 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC