943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 27533
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 28th, 2004
1

Pi Approximation

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
essentialc++33 is offline Offline
7 posts
since Mar 2004
Mar 28th, 2004
0

Re: programming

write some code, and then ask us for help when u get stuck. read the sticky about homework.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 28th, 2004
0

Re: Pi Approximation

Quote originally posted by essentialc++33 ...
Approximation value of pi is 3.466667.
Don't you mean ~3.1415 ... ??
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Mar 28th, 2004
0

Re: Pi Approximation

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
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Mar 28th, 2004
0

Re: Pi Approximation

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
essentialc++33 is offline Offline
7 posts
since Mar 2004
Mar 28th, 2004
0

Re: programming

Quote 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;

}

Reputation Points: 11
Solved Threads: 0
Newbie Poster
essentialc++33 is offline Offline
7 posts
since Mar 2004
Mar 28th, 2004
0

Re: Pi Approximation

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.
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Mar 28th, 2004
0

Re: Pi Approximation

Quote 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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
essentialc++33 is offline Offline
7 posts
since Mar 2004
Mar 28th, 2004
-1

Re: Pi Approximation

usingnamespace std; use that with <iostream> and it should work.
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Mar 28th, 2004
0

Re: Pi Approximation

Quote 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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
essentialc++33 is offline Offline
7 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Hang Man
Next Thread in C++ Forum Timeline: find out if array is sorted or not





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC