How many terms to find p of Pi?

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

Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

How many terms to find p of Pi?

 
0
  #1
Oct 21st, 2008
I was assigned a homework project that's starting to get annoying. I can't figure out what's going wrong with it. Here's the question:

The number Pi may be calculated using the following infinite series:
Pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... )
How many terms of this series you need to use before you get the approximation p of Pi, where:
a)p = 3.0
b)p = 3.1
c)p = 3.14
d)p = 3.141
e)p = 3.1415
f)p = 3.14159
Write a C++ program to answer this question.

My answer is always 4 and my term starts at 0 and stays at 1. Here's what I have so far:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double pif(double); //Fucntion to determine value of pi
  7. int term; //Variable to count terms
  8.  
  9. int main()
  10. {
  11. cout << "Value of Pi" << " " << "Number of terms" << endl;
  12.  
  13. cout << pif(3.0) << " " << term << endl;
  14. cout << pif(3.1) << " " << term << endl;
  15. cout << pif(3.14) << " " << term << endl;
  16. cout << pif(3.141) << " " << term << endl;
  17. cout << pif(3.1415) << " " << term << endl;
  18. cout << pif(3.14159) << " " << term << endl;
  19.  
  20. return 0;
  21. }
  22.  
  23. double pif(double n)
  24. {
  25. double pi = 0.0; //Variable to store value of pi
  26. int sign = 1; //Variable to store sign
  27. bool check = false; //Variable to check value of pi
  28.  
  29. term = 0;
  30.  
  31. while (!check)
  32. {
  33. if (pi * 4.0 >= n) //If value of pi is greater than or equal to approx of pi
  34. check = true; //Then exit the loop
  35. else
  36. {
  37. pi *= sign * (1.0 / (1.0 + term * 2.0)); //Otherwise calculate value of pi
  38. sign *= -1; //Change sign
  39. ++term; //And increment term
  40. }
  41. }
  42.  
  43. pi *= 4.0; //Perform final pi calculation after fractional sums have been determined
  44.  
  45. return pi;
  46. }

Thanks for the help!
Last edited by ShadowOfBlood; Oct 21st, 2008 at 9:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How many terms to find p of Pi?

 
1
  #2
Oct 21st, 2008
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double pif(double); //Fucntion to determine value of pi
  7. int term; //Variable to count terms
  8.  
  9. int main()
  10. {
  11. cout << "Value of Pi" << " " << "Number of terms" << endl;
  12.  
  13. cout << pif(3.0) << " " << term << endl;
  14. cout << pif(3.1) << " " << term << endl;
  15. cout << pif(3.14) << " " << term << endl;
  16. cout << pif(3.141) << " " << term << endl;
  17. cout << pif(3.1415) << " " << term << endl;
  18. cout << pif(3.14159) << " " << term << endl;
  19.  
  20. return 0;
  21. }
  22.  
  23. double pif(double n)
  24. {
  25. double pi = 0.0; //Variable to store value of pi
  26. int sign = 1; //Variable to store sign
  27. bool check = false; //Variable to check value of pi
  28.  
  29. term = 0;
  30.  
  31. while (!check)
  32. {
  33. if (pi * 4.0 >= n) //If value of pi is greater than or equal to approx of pi
  34. check = true; //Then exit the loop
  35. else
  36. {
  37. pi *= sign * (1.0 / (1.0 + term * 2.0)); //Otherwise calculate value of pi
  38. sign *= -1; //Change sign
  39. ++term; //And increment term
  40. }
  41. }
  42.  
  43. pi *= 4.0; //Perform final pi calculation after fractional sums have been determined
  44.  
  45. return pi;
  46. }

Line 37: Why are you multiplying here? You should be adding or subtracting:

Pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... )
Line 33: This is an incorrect test. You need to compare the value of pi that you have calculated to the value passed to the function and see whether it is within a certain range. For example, when checking against 3.141, you are probably checking whether your calculated value of pi is within 1/1000 of 3.141. For 3.1415, you are probably checking whether your calculated values is within 1/10,000 of 3.1415. Thus you should probably round OFF the numbers before passing them to the function (i.e. pass 3.1416 rather than 3.1415 to the function).

Line 45: You should be returning the number of iterations, not pi, right? The whole idea is to find out how many iterations it takes to get within the proper tolerance.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How many terms to find p of Pi?

 
0
  #3
Oct 22nd, 2008
Thanks, I didn't even realize I used multiplication in line 37. Also, how do I round off the numbers?
Last edited by ShadowOfBlood; Oct 22nd, 2008 at 12:53 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: broke-chanic is an unknown quantity at this point 
Solved Threads: 0
broke-chanic broke-chanic is offline Offline
Newbie Poster

Re: How many terms to find p of Pi?

 
0
  #4
Oct 22nd, 2008
Hey Bro, try this-

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. double pi;
  6. double divisor;
  7.  
  8. int term;
  9. term = 0;
  10. divisor = 3.0;
  11. pi = 4.0
  12.  
  13.  
  14. for ( ; 1750 > term; term++)
  15. /* You can change the "1750" to whatever. That is just how many terms I wanted to show*/
  16.  
  17. {
  18. divisor = ( 2 * term) + 3.0;
  19.  
  20. if (term % 2 == 0)
  21. {
  22. pi = pi - 4.0 / divisor
  23. }
  24.  
  25. else
  26. {
  27. pi = pi + 4.0 / divisor;
  28. }
  29.  
  30. printf("%lf term%d\n", pi, term);
  31.  
  32. }
  33.  
  34. system("pause") /* I know that's bad but I can get it to stop automatically yet*/
  35.  
  36. return 0;
  37. }


It works. I just figured it out this past Sunday.. I am new to the programming world so my "style may not be too good..
Last edited by Ancient Dragon; Oct 22nd, 2008 at 1:50 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How many terms to find p of Pi?

 
1
  #5
Oct 22nd, 2008
It works. I just figured it out this past Sunday.. I am new to the programming world so my "style may not be too good..
Your using C syntax in a C++ forum....

Also use [code][/code] please

@OP
rounding it off can be done by adding 0.0005 etc which which ever number of zero's it requires

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How many terms to find p of Pi?

 
0
  #6
Oct 22nd, 2008
Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How many terms to find p of Pi?

 
0
  #7
Oct 22nd, 2008
I still can't get it to tell me how many terms ><

It's still saying 1 term for each approximation.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How many terms to find p of Pi?

 
0
  #8
Oct 22nd, 2008
Originally Posted by ShadowOfBlood View Post
I still can't get it to tell me how many terms ><

It's still saying 1 term for each approximation.
Post your updated code. We don't know what you've changed.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC