943,772 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2838
  • C++ RSS
Mar 15th, 2007
0

How do you code a series?

Expand Post »
I find a problem which says

An approximate value of pi can be calculated using the series given below:
Pi = 4 [ 1 – 1/3 + 1/5 – 1/7 + 1/9 … + ((-1)n)/(2n+1)]
Write a C++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation. Includes a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.

As far as I know, the we can use the formula given in the problem, put it in the loop and using input from the user to determine when it should end. But I can't code it at all http://images.devshed.com/fds/smilies/mad.gif I mean... its like a series that keeps on going if not mistaken. How can I code this? Plus, it swtiches from + to - repeatedly. zz confused.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ice_tea_lemon is offline Offline
7 posts
since Mar 2007
Mar 16th, 2007
0

Re: How do you code a series?

C version: returns pi/4
C++ Syntax (Toggle Plain Text)
  1. #include <stdlib.h>
  2.  
  3. double pi_over_4(int n)
  4. {
  5. double iter=3;
  6. int eveodd=1;
  7. double pi=0.;
  8.  
  9. for(pi=1.; n>0; n--, iter+=2., eveodd=!eveodd)
  10. {
  11. if(eveodd)
  12. pi-=1./iter;
  13. else
  14. pi+=1./iter;
  15. }
  16. return pi;
  17. }
Last edited by jim mcnamara; Mar 16th, 2007 at 3:37 pm.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Oct 26th, 2009
-2
Re: How do you code a series?
wow...what would this same code be like in c++? i want to start c++ as a hobby. xd
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nekomata is offline Offline
2 posts
since Oct 2009

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: BigInt addition: Is this correct?
Next Thread in C++ Forum Timeline: c++ approx pi notes :(





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


Follow us on Twitter


© 2011 DaniWeb® LLC