954,184 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pi Approximation

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
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
Solved Threads: 0
 

write some code, and then ask us for help when u get stuck. read the sticky about homework.

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 
Approximation value of pi is 3.466667.


Don't you mean ~3.1415 ... ??

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

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

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

the book sample shows 3.466667.
this is what I wrote:
#include
#include

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
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
Solved Threads: 0
 
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

#include

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;

}

essentialc++33
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
Solved Threads: 0
 

you need to display pi when it has been approximated:

cout << "approximate value of pi is"<#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.

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

you need to display pi when it has been approximated: cout << "approximate value of pi is"<#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 does not exist.
do you think i should not use that, i tried i still get error.

essentialc++33
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
Solved Threads: 0
 

usingnamespace std; use that with and it should work.

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 
usingnamespace std; use that with 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 i removed that too.

essentialc++33
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
Solved Threads: 0
 

well .h is deprecated but none the less still there.

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 
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
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
Solved Threads: 0
 

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

usingnamespace 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;

cin >> s.numer >> s.denom;

while (s.numer, s.denom < 0)

{

cout <<"Yes you also enter a negative integer" <b);

return find gcd (s, r/s);

elsereturn 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

essentialc++33
Newbie Poster
7 posts since Mar 2004
Reputation Points: 11
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.

#include
#include
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..:)

loga88
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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
#include
#include
#include

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: "<

beginner_199
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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.

raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You