Okay so I'm kinda stumped... here are our directions.. My code is at the bottom of this post

"Write a program consisting of only the main function, called piApproximator.cpp. When your program begins, the user is prompted to enter a number n representing the number of terms to be used in the approximation. Your
program should then compute the series approximation of π using the ﬁrst n terms of the series described above and display that approximation."

The series is π = Summation: (-1)^(i+1)*[4/(2i-1)] = 4 [1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11....]

Sample Run 3:
This program approximates pi using an n-term series expansion.
Enter the value of n> 6
pi[6] = 4[1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11] = 2.97604617604617560644

Sample Run 4:
This program approximates pi using an n-term series expansion.
Enter the value of n> 10
pi[10] = 4[1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 - 1/19] = 3.04183961892940146754

okay so that is what the output is supposed to look like..
here is my code:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main ()
{
double pi = 0;
double i;
int n;
cout<<"This program approximates pi using an n-term series expansion."<<endl;
cout<<"Enter the value of n>";
cin>>i;
{
if (n<=0)
cout<<" "<<n<<" is a invalid number of terms."<<endl;
}
for (long int n = 1; n <= i; n++)
{
pi += (double) pow(-1, n+1)*(4/(2*n-1));
}
pi *= 4;
cout<<"pi"<<"["<<n<<"]"<<" = "<<pi;

return 0;
}


## All 12 Replies

Whats the problem

My output looks like this:

Enter the value of n>3
pi[3] = 12


For any number I put as the value of n... it gives me the answer 12.. just confused as to what I'm doing wrong.. My output should also show the terms used .. like if my n (number of terms) was 3.. then it should show:

pi[3] = 4[1 - 1/3 + 1/5] = (my answer)

For any number I put as the value of n... it gives me the answer 12..

My output should also show the terms used .. like if my n (number of terms) was 3.. then it should show:

pi[3] = 4[1 - 1/3 + 1/5] = (my answer)

This could be solved by using cout in your for loop

Thank you for contribution! but.. if I can try to get a little bit more of a hint as to what cout should say.. I'm at a huge mental block because I think everything is right when I know it isn't =/

pow excepts doubles doesn't it?

Additionally why are you doing this pi *= 4; when you are already multiplying by four in the for loop?

It was just a lapse of thought to remove it from the program. However, I'm still getting the answer 12.. Is there anyway you can give me a hint to display the numbers as 1 + 1/3 - 1/5 + 1/7.. etc?

It was just a lapse of thought to remove it from the program. However, I'm still getting the answer 12.

If you comment out the line pi *=4; you should get a value such as 3.14 for a sufficiently large value of 'n'.

I'm not sure what compiler you're using but I got a few errors. One of the things I did was to declare n as a double and use pow(-1.0,n+1)... instead of pow(-1,n+1)

As you can see I've started to throw in a few couts in the for loop to start actually printing the series. It isn't complete but should give you a direction to follow. Try it and see what you get.

for (double n = 1; n <= i; n++)
{
pi += (double) pow(-1.0, n+1)*(4/(2*n-1));

cout << pow(-1.0,n+1);
cout << " /" <<(2*n-1);
}


Thank you so much

(4/(2*n-1) results in integer division.
Instead, you need something like 4.0 / ( 2*n - 1 )

To get alternative plus and minus signs, we do not need the expensive and approximate std::pow().

Something like this:

// pi = 4 * ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... )

int sign = +1 ;
double pi = 0.0 ;
constexpr int n = 1000000 ;

for( int i = 0 ; i < n ; ++i )
{
pi += sign * 4.0 / ( i*2 + 1 ) ;
sign = -sign ;
}

std::cout << std::fixed << pi << '\n' ;


Here this should fix it:

    for (double n = 1; n <= i; n++){
pi += pow(-1, n+1)*(4/(2*n-1));
}


Pow requires double as arguments, so you'll need to make your variable from the loop a double.
Another thing you could do, when you try to print real numbers, is to set the precision to your cout (number of digits to show after the '.').

    cout.precision(20);
cout<<"pi"<<"["<<i<<"]"<<" = "<<pi;


Have a look at the cout.precision() method Click Here.

From your code, my output looks like this:

/*
*  Output
*/

This program approximates pi using an n-term series expansion.
Enter the value of n>10
pi[10] = 3.0418396189294014675

This program approximates pi using an n-term series expansion.
Enter the value of n>15
pi[15] = 3.2081856522619416339


Also, I let myself edit some things inside of your program, to make it more clear:

#include <iostream>
#include <cmath>
using namespace std;

int main (){
double pi = 0, n;
cout<<"This program approximates pi using an n-term series expansion.\n"
<<"Enter the value of n>";
cin>>n;
for (double i = 1; i <= n; i++){
pi += pow(-1, i+1)*(4/(2*i-1));
}
cout.precision(20);
cout<<"pi"<<"["<<n<<"]"<<" = "<<pi;
return (0);
}


whats the series expansion of pi power4 divided by 180

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.