•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,497 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,456 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1339 | Replies: 5
![]() |
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
We were assigned the following problem as a large project in my Freshman intro to C++ programming class:
We have to determine the number of terms needed to calculate Pi to a specific number (3.0, 3.1, 3.14, 3.141, 3.1415, and 3.14159), using the formula:
pi= 4(1-1/3+1/5-1/7+1/9-1/11+1/13...)
I thought I had the code figured out pretty easily, but I have having a problem. I was going to use the following (incomplete) code as a model:
In the main function there, I was going to try to have it call the function pif() once for each value q that I set to it, then cout the results, and the number of terms.
I've been toying around with possible options (as seen in the while statement there), but no matter what I do, it returns a value of o terms and 0.00 for pi each time.
What am I doing wrong?
Thanks in advance, this is killing me.
We have to determine the number of terms needed to calculate Pi to a specific number (3.0, 3.1, 3.14, 3.141, 3.1415, and 3.14159), using the formula:
pi= 4(1-1/3+1/5-1/7+1/9-1/11+1/13...)
I thought I had the code figured out pretty easily, but I have having a problem. I was going to use the following (incomplete) code as a model:
// Date: 10/30/06
// Homework #07
// Student: Jim Hummel
// Course: CSE 121
// This program calculates the number of terms required for an approximation of pi.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double pif(double q);
int terms=0;
int main()
{double q=3.1;
cout << "Pi Value Terms\n--------------------------\n";
cout << fixed << setprecision(2);
cout << pif(q)<< " "<< terms << endl;
return 0;
}
double pif (double q)
{double n=1.0;
double pi=0;
while (pi<(q-(1.0/q)) && pi>(q+(1.0/q)))
{
pi = pow(-1.0,n)/(2.0*n-1.0);
n++;
terms++;
}
pi*=4.0;
return pi;
}In the main function there, I was going to try to have it call the function pif() once for each value q that I set to it, then cout the results, and the number of terms.
I've been toying around with possible options (as seen in the while statement there), but no matter what I do, it returns a value of o terms and 0.00 for pi each time.
What am I doing wrong?
Thanks in advance, this is killing me.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Sorry, q was to represent the question.
I was going to have it so that I would set q to 3.0 and send it to pif()... then cout the results, and then set q to 3.1... repeat with 3.14, 3.141, all the way to 3.14159, which is as far as I need to go. I need to know the number of terms used for each of those values (how many fractions were necessary to get pi to equal that number.)
Thanks.
I was going to have it so that I would set q to 3.0 and send it to pif()... then cout the results, and then set q to 3.1... repeat with 3.14, 3.141, all the way to 3.14159, which is as far as I need to go. I need to know the number of terms used for each of those values (how many fractions were necessary to get pi to equal that number.)
Thanks.
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Maybe this could help: This is the exact problem:
Problem 2. [The Number p] The number p may be calculated using the following infinite series:
p = 4(1-1/3+1/5-1/7+1/9-1/11+1/3
How many terms of this series you need to use before you get the approximation p of p, 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.
Problem 2. [The Number p] The number p may be calculated using the following infinite series:
p = 4(1-1/3+1/5-1/7+1/9-1/11+1/3
How many terms of this series you need to use before you get the approximation p of p, 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.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
I don't understand why you are testing how close the value calculated is to 1/q. But anyway, here is a more refined way of writing the source. The algorithm is the same, so any errors are due to that.
Do not use global variables unless absolutely necessary.
Do not use global variables unless absolutely necessary.
c Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> #include <iomanip> using namespace std; double pif(double q); int terms=0; int main() { double q=3.1; cout << "Pi Value Terms\n--------------------------\n"; cout.setf( ios::fixed ); cout.precision (2); cout << pif(q)<< " "; cout << terms << endl; // Since you are changing the value of terms in the above line, // do not output it in the same line. The result is undefined. // Use a seperate line to output terms. // But a better way will be to output the value of n in pif() return 0; } double pif (double approx) // Use meaningful parameter names rather than just p, q, r { long n=0; double pi=0.0; double error = fabs( pi - approx ); double tolerance = 1 / approx; // Assuming approx is positive, this is also +ve // while (pi<(q-(1.0/q)) && pi>(q+(1.0/q)))// Actually this should be // while (pi<(q-(1.0/q)) || pi>(q+(1.0/q))). // It is the same as | pi -q | > ( 1.0 / q ) // |x| = absolute value of x while ( error > tolerance ) { n++; term++; pi = pi + pow( -1, n + 1)/(2 * n - 1 ); error = fabs( pi - approx); } pi*=4.0; // cout << pi << " "<< n << " " << error << endl; return pi; }
バルサミコ酢やっぱいらへんで
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks for the reply, although I didn't read it in time before I had to hand it in.
I worked on building an entirely new code without the rediculous while statements... it led to a very long, messy, and unoganized code, but it worked.
I spent the remainder of my time trying to turn it into a while loop, using another variable for the setprecision (as seen below) and the q-(number) statements, but this code here did work and got the job done:
I worked on building an entirely new code without the rediculous while statements... it led to a very long, messy, and unoganized code, but it worked.
I spent the remainder of my time trying to turn it into a while loop, using another variable for the setprecision (as seen below) and the q-(number) statements, but this code here did work and got the job done:
c Syntax (Toggle Plain Text)
// Date: 10/30/06 // Homework #07 // Student: Jim Hummel // Course: CSE 121 // This program calculates the number of terms required for an approximation of pi. #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() {double q=3.0; double n=1; bool check=false; double pi = 0; int terms=0; while (!check) { pi += (double) pow(-1, n+1)/(2*n-1); cout << fixed << setprecision(1); if (q-.1<=pi*4 && q+.1>=pi*4) check=true; else terms++; n++; } pi *= 4; cout << terms << " " << pi << endl; //Part 2// q=3.1; n=1; check=false; pi = 0; terms=0; while (!check) { pi += (double) pow(-1, n+1)/(2*n-1); cout << fixed << setprecision(1); if (q-.01<=pi*4 && q+.01>=pi*4) check=true; else terms++; n++; } pi *= 4; cout << terms << " " << pi << endl; //Part 3// q=3.14; n=1; check=false; pi = 0; terms=0; while (!check) { pi += (double) pow(-1, n+1)/(2*n-1); cout << fixed << setprecision(2); if (q-.001<=pi*4 && q+.001>=pi*4) check=true; else terms++; n++; } pi *= 4; cout << terms << " " << pi << endl; //Part 4// q=3.141; n=1; check=false; pi = 0; terms=0; while (!check) { pi += (double) pow(-1, n+1)/(2*n-1); cout << fixed << setprecision(3); if (q-.0001<=pi*4 && q+.0001>=pi*4) check=true; else terms++; n++; } pi *= 4; cout << terms << " " << pi << endl; //Part 5// q=3.1415; n=1; check=false; pi = 0; terms=0; while (!check) { pi += (double) pow(-1, n+1)/(2*n-1); cout << fixed << setprecision(4); if (q-.00001<=pi*4 && q+.00001>=pi*4) check=true; else terms++; n++; } pi *= 4; cout << terms << " " << pi << endl; //Part 6// q=3.14159; n=1; check=false; pi = 0; terms=0; while (!check) { pi += (double) pow(-1, n+1)/(2*n-1); cout << fixed << setprecision(5); if (q-.000001<=pi*4 && q+.000001>=pi*4) check=true; else terms++; n++; } pi *= 4; cout << terms << " " << pi << endl; return 0; }
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Newbie - looping and array [very simple] problem (PHP)
- Searching a record (Pascal and Delphi)
- Help me with this Simple Problem plss (C)
- Shell Scripting problem and related awk issues (Shell Scripting)
- BUTTON DOES NOT WORK??? simple problem (C)
- include file problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Inserting objects into an array
- Next Thread: New object initialization error



Linear Mode