943,911 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1674
  • C++ RSS
Oct 21st, 2008
0

How many terms to find p of Pi?

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 21st, 2008
1

Re: How many terms to find p of Pi?

C++ Syntax (Toggle Plain Text)
  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:

Quote ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 22nd, 2008
0

Re: How many terms to find p of Pi?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 22nd, 2008
0

Re: How many terms to find p of Pi?

Hey Bro, try this-

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
broke-chanic is offline Offline
1 posts
since Oct 2008
Oct 22nd, 2008
1

Re: How many terms to find p of Pi?

Quote ...
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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 22nd, 2008
0

Re: How many terms to find p of Pi?

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 22nd, 2008
0

Re: How many terms to find p of Pi?

I still can't get it to tell me how many terms ><

It's still saying 1 term for each approximation.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Oct 22nd, 2008
0

Re: How many terms to find p of Pi?

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

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: can someone help me out with this project pleeeeease.
Next Thread in C++ Forum Timeline: Write from CPP program to serial port





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


Follow us on Twitter


© 2011 DaniWeb® LLC