| | |
How do you code a series?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
C version: returns pi/4
C++ Syntax (Toggle Plain Text)
#include <stdlib.h> double pi_over_4(int n) { double iter=3; int eveodd=1; double pi=0.; for(pi=1.; n>0; n--, iter+=2., eveodd=!eveodd) { if(eveodd) pi-=1./iter; else pi+=1./iter; } return pi; }
Last edited by jim mcnamara; Mar 16th, 2007 at 3:37 pm.
![]() |
Similar Threads
- Collins: Why this scientist believes in God (Geeks' Lounge)
- beginning c++ (C++)
- Homework help ...sort of (Java)
- Graphics in Pixel,Mode13h:Part 1 (C++)
- sum of series (C++)
Other Threads in the C++ Forum
- Previous Thread: BigInt addition: Is this correct?
- Next Thread: c++ approx pi notes :(
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





