| | |
Pi Approximation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2004
Posts: 7
Reputation:
Solved Threads: 0
The value for PI can be determined by the series equation
PI=4x(1-1/3+1/5-1/7+1/9-1/11+1/13-....)
write an interactive program that asks the user how manu terms of series
equation to use in approximating PI. Then calculate and display the
approximation. Here is a sample run:
PI Approximation Program
How many terms of the series should be included?
(The more terms, the better the approximation)
=> 3
Approximation value of pi is 3.466667.
PI=4x(1-1/3+1/5-1/7+1/9-1/11+1/13-....)
write an interactive program that asks the user how manu terms of series
equation to use in approximating PI. Then calculate and display the
approximation. Here is a sample run:
PI Approximation Program
How many terms of the series should be included?
(The more terms, the better the approximation)
=> 3
Approximation value of pi is 3.466667.
•
•
•
•
Originally Posted by essentialc++33
Approximation value of pi is 3.466667.
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
Join Date: Mar 2004
Posts: 7
Reputation:
Solved Threads: 0
the book sample shows 3.466667.
this is what I wrote:
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
double pi =3.14159265358979323846; //approximate value of pi.
chardouble terms;
pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...);
cout << "Please enter how many terms of the series equation to use in approximating pi"<< endl;
cin >> enter terms;
cout << "approximate value of pi is"<< endl;
return 0;
}
I don't know what I am missing or what esle to do.
this is what I wrote:
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
double pi =3.14159265358979323846; //approximate value of pi.
chardouble terms;
pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...);
cout << "Please enter how many terms of the series equation to use in approximating pi"<< endl;
cin >> enter terms;
cout << "approximate value of pi is"<< endl;
return 0;
}
I don't know what I am missing or what esle to do.
Last edited by essentialc++33; Mar 28th, 2004 at 3:52 pm. Reason: send the copy for what i wrote
•
•
Join Date: Mar 2004
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by infamous
write some code, and then ask us for help when u get stuck. read the sI don't know what I am missing or what esle to do.ticky about homework.
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
double pi =3.14159265358979323846; //approximate value of pi.
chardouble terms;
pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...);
cout << "Please enter how many terms of the series equation to use in approximating pi"<< endl;
cin >> enter terms;
cout << "approximate value of pi is"<< endl;
return 0;
}
you need to display pi when it has been approximated:
cout << "approximate value of pi is"<<pi<<endl;
and pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...); will not work becuase the compiler can not calculate the ... so to coninue you would need some sort of looped structure. Then when it has done that, all you have to do is multiply by 4.
heres an example of how i would do it:
As you can see I left some work in there for you, but it up to you to find out how the program works and what you need to modify to allow the user to determin the number of cycles.
cout << "approximate value of pi is"<<pi<<endl;
and pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...); will not work becuase the compiler can not calculate the ... so to coninue you would need some sort of looped structure. Then when it has done that, all you have to do is multiply by 4.
heres an example of how i would do it:
#include <iostream.h> #include <math.h> int main() { double pi = 0; // Calculating pi/4 int elements = 2000; for (long int n = 1; n <= elements; n++) { pi += (double) pow(-1, n+1)/(2*n-1); } // Calculating pi pi *= 4; cout << "Estimated PI value (" << elements << " elements): "<< pi; return 0; }
As you can see I left some work in there for you, but it up to you to find out how the program works and what you need to modify to allow the user to determin the number of cycles.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
•
•
Join Date: Mar 2004
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by BountyX
you need to display pi when it has been approximated:
cout << "approximate value of pi is"<<pi<<endl;
and pi =4x(1-1/3+1/5-1/7-+1/9-1/11+1/13/...); will not work becuase the compiler can not calculate the ... so to coninue you would need some sort of looped structure. Then when it has done that, all you have to do is multiply by 4.
heres an example of how i would do it:
#include <iostream.h> #include <math.h> int main() { double pi = 0; // Calculating pi/4 int elements = 2000; for (long int n = 1; n <= elements; n++) { pi += (double) pow(-1, n+1)/(2*n-1); } // Calculating pi pi *= 4; cout << "Estimated PI value (" << elements << " elements): "<< pi; return 0; }
As you can see I left some work in there for you, but it up to you to find out how the program works and what you need to modify to allow the user to determin the number of cycles.
do you think i should not use that, i tried <isotream> i still get error.
![]() |
Similar Threads
- minimum weight perfect matching (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ and Excel
- Next Thread: Preventing Hooks
Views: 20410 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






