| | |
How many terms to find p of Pi?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
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:
Thanks for the help!
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)
#include <iostream> #include <cmath> using namespace std; double pif(double); //Fucntion to determine value of pi int term; //Variable to count terms int main() { cout << "Value of Pi" << " " << "Number of terms" << endl; cout << pif(3.0) << " " << term << endl; cout << pif(3.1) << " " << term << endl; cout << pif(3.14) << " " << term << endl; cout << pif(3.141) << " " << term << endl; cout << pif(3.1415) << " " << term << endl; cout << pif(3.14159) << " " << term << endl; return 0; } double pif(double n) { double pi = 0.0; //Variable to store value of pi int sign = 1; //Variable to store sign bool check = false; //Variable to check value of pi term = 0; while (!check) { if (pi * 4.0 >= n) //If value of pi is greater than or equal to approx of pi check = true; //Then exit the loop else { pi *= sign * (1.0 / (1.0 + term * 2.0)); //Otherwise calculate value of pi sign *= -1; //Change sign ++term; //And increment term } } pi *= 4.0; //Perform final pi calculation after fractional sums have been determined return pi; }
Thanks for the help!
Last edited by ShadowOfBlood; Oct 21st, 2008 at 9:30 pm.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; double pif(double); //Fucntion to determine value of pi int term; //Variable to count terms int main() { cout << "Value of Pi" << " " << "Number of terms" << endl; cout << pif(3.0) << " " << term << endl; cout << pif(3.1) << " " << term << endl; cout << pif(3.14) << " " << term << endl; cout << pif(3.141) << " " << term << endl; cout << pif(3.1415) << " " << term << endl; cout << pif(3.14159) << " " << term << endl; return 0; } double pif(double n) { double pi = 0.0; //Variable to store value of pi int sign = 1; //Variable to store sign bool check = false; //Variable to check value of pi term = 0; while (!check) { if (pi * 4.0 >= n) //If value of pi is greater than or equal to approx of pi check = true; //Then exit the loop else { pi *= sign * (1.0 / (1.0 + term * 2.0)); //Otherwise calculate value of pi sign *= -1; //Change sign ++term; //And increment term } } pi *= 4.0; //Perform final pi calculation after fractional sums have been determined return pi; }
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 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.
•
•
Join Date: Oct 2008
Posts: 1
Reputation:
Solved Threads: 0
Hey Bro, try this-
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..
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main() { double pi; double divisor; int term; term = 0; divisor = 3.0; pi = 4.0 for ( ; 1750 > term; term++) /* You can change the "1750" to whatever. That is just how many terms I wanted to show*/ { divisor = ( 2 * term) + 3.0; if (term % 2 == 0) { pi = pi - 4.0 / divisor } else { pi = pi + 4.0 / divisor; } printf("%lf term%d\n", pi, term); } system("pause") /* I know that's bad but I can get it to stop automatically yet*/ return 0; }
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
•
•
•
•
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..
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
![]() |
Similar Threads
- Internet Speeds (Web Browsers)
- Google image game (Posting Games)
- Help a newbie find JSP tutorials (was: I'm a newbe, help!) (JSP)
- Forum Registration Terms and Conditions (Growing an Online Community)
- My Hijackthis Log - IE cannot find server after SP2 (Viruses, Spyware and other Nasties)
- Having trouble with usb driver (Windows 95 / 98 / Me)
- how to find stock information with php script (PHP)
- Cannot get rid of "Cannot Find Server" message (Viruses, Spyware and other Nasties)
- Internet Explorer has been Hijacked. (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: can someone help me out with this project pleeeeease.
- Next Thread: Write from CPP program to serial port
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib 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






