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.
essentialc++33
- 7 Contributors
- forum15 Replies
- 19 Views
- 14 Years Discussion Span
- comment Latest Post by raptr_dflo
infamous 26
write some code, and then ask us for help when u get stuck. read the sticky about homework.
Dani 1,665
Approximation value of pi is 3.466667.
Don't you mean ~3.1415 ... ??
BountyX 7
I hope he does, 3.4 is a tad bit off .... hey try to do the project on your own and when you get absolutley then specify your exact problem and post some code then we can help you out :D
essentialc++33
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.
essentialc++33
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.
This is what I wrote: and I don't know where else to go.
#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;
}
BountyX 7
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.
Edited
by pyTony: fixed formatting
essentialc++33
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.
I modify it the way you told me too and send ask the user how to enter info. but the program tells me that path <iostream.h> does not exist.
do you think i should not use that, i tried <isotream> i still get error.
Edited
by pyTony: fixed formatting
BountyX 7
usingnamespace std; use that with <iostream> and it should work.
essentialc++33
usingnamespace std; use that with <iostream> and it should work.
when i have usingnamespace std; there already and it is stil not working? it gave me more errors than before.
it says about <math.h> i removed that too.
BountyX 7
well .h is deprecated but none the less still there.
essentialc++33
well .h is deprecated but none the less still there.
Thank you guys. You gave me a big hint. I finally got it to work.
see you guys next time. it is a wonderfully you are doing.
thank you again.
essentialc++33
the third program is one that adds and subtracts two fractions, not necessarily positive, and displays the exact sum or difference in the lowest terms.
Use the c++ construct "struct" to create a data type for the fractions.
This how I do this;
#include <iostream>
using namespace std;
struct frac{int numer, denom;};
frac f_add( frac, frac );
int main( ){
frac r, s, sum;
cout << "Enter the numerator and the denominator of the first fraction separated by a space=> " << endl;
cin >> r.numer >> r.denom;
while (r.numer,r.denom < 0)
{
cout <<"Yes you may enter a negative integer" <<endl;
}
cout << "Enter the numerator and the denominator of the second fraction separate by a space=> " << endl;
cin >> s.numer >> s.denom;
while (s.numer, s.denom < 0)
{
cout <<"Yes you also enter a negative integer" <<endl;
}
cout <<"You have entered "<< r.numer <<" / "<< r.denom << " and " << s.numer<<"/" <<s.denom<<"\n"<<endl;
int gcd = find gcd (r.denom, s.denom);
sum = f_add(r,s);
cout << "The sum of " << r.numer <<"/" << r.denom << " and "<< s.numer <<"/" << s.denom <<" in lowest term is " << sum.numer <<"/" << sum.denom <<".\n";
return 0;
}
frac f_add(frac r, frac s){
if (r=s)
return r;
else if (a=0)
return s;
else if (s=0)
return r;
else if (r>b);
return find gcd (s, r/s);
else
return findgcd (r, s/r);
}
}
I feel like i am doing something wrong but i cannot figure it out! Please help!
I am very confused with the construct "struct":
what do think i need and how to apply it and where?
thank you
loga88
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.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double pi = 0; // Calculating pi/4
int elements;
cout<<"how many...??";
cin>>elements;
for (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;
}
try tis the right answer..:)
Edited
by mike_2000_17: Fixed formatting
beginner_199
This is how i would write the pi approximation, there might be some errors(might), might be a little confusing but this is how i can understand it
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
void get_pie(double& pie_answer); //function prototype
void magic_code();
void return_pie(double pie_answer);
int main()
{
double pie_answer; //declarations
void magic_code(); //getting magic code
get_pie(pie_answer); //calculating the answer of pi
return_pie(pie_answer);
getchar();
return 0;
}
//**********************************************************************
//this function will give the magic code to the main program
void magic_code()
{
cout.setf(ios::fixed); //magic code
cout.setf(ios::showpoint);
cout.precision(5);
}
//**********************************************************************
//this function will calculate the answer to pi
void get_pie(double& pie_answer);
{
int denom1=1,denom2=3,pos,neg,count; //declarations
for(count=1; count<=100; count++)
{
pos+=1/denom1;
denom1 += 4;
neg+=1/denom2;
denom2 += 4;
}
pie_answer= 4(pos-neg);
}
//this function will output the answer of pi
void return_pie(double pie_answer)
{
cout<<"The approximation of pie is: "<<pie_answer; //this function will output pi
}
Edited
by Nick Evan: Fixed formatting
raptr_dflo 48
Welcome beginner,
There might be some errors.
Might? Did you run your own program and see what it outputs? (Hint: there are several errors in your get_pie() function involving data types.) Also, please use [ CODE ] tags (select your code after you paste it, and click the [ CODE ] button/icon at the top of the editor) so your program is more readable.
Did you have a specific question to ask? And in the future, please don't post into long-dead threads.