I was assigned a homework project that's starting to get annoying. I can't figure out what's going wrong with it. Here's the question:

The number Pi may be calculated using the following infinite series:
Pi = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... )
How many terms of this series you need to use before you get the approximation p of Pi, 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.

My answer is always 4 and my term starts at 0 and stays at 1. Here's what I have so far:

``````#include <iostream>
#include <cmath>

using namespace std;

double pif(double); //Fucntion to determine value of pi
int term; //Variable to count terms

int main()
{
cout << "Value of Pi" << "		" << "Number of terms" << endl;

cout << pif(3.0) << "			" << term << endl;
cout << pif(3.1) << "			" << term << endl;
cout << pif(3.14) << "			" << term << endl;
cout << pif(3.141) << "			" << term << endl;
cout << pif(3.1415) << "			" << term << endl;
cout << pif(3.14159) << "			" << term << endl;

return 0;
}

double pif(double n)
{
double pi = 0.0; //Variable to store value of pi
int sign = 1; //Variable to store sign
bool check = false; //Variable to check value of pi

term = 0;

while (!check)
{
if (pi * 4.0 >= n) //If value of pi is greater than or equal to approx of pi
check = true; //Then exit the loop
else
{
pi *= sign * (1.0 / (1.0 + term * 2.0)); //Otherwise calculate value of pi
sign *= -1; //Change sign
++term; //And increment term
}
}

pi *= 4.0; //Perform final pi calculation after fractional sums have been determined

return pi;
}``````

Thanks for the help!

## Recommended Answers

``````#include <iostream>
#include <cmath>

using namespace std;

double pif(double); //Fucntion to determine value of pi
int term; //Variable to count terms

int main()
{
cout << "Value of Pi" << "		" << "Number of terms" << endl;

cout << pif(3.0) << "			" << term << …``````

It works. I just figured it out this past Sunday.. I am new to the programming world so my "style may not be too good..

Your using C syntax in a C++ forum....

Also use [code][/code] please

@OP
rounding it off can be done by adding 0.0005 etc …

## All 7 Replies

``````#include <iostream>
#include <cmath>

using namespace std;

double pif(double); //Fucntion to determine value of pi
int term; //Variable to count terms

int main()
{
cout << "Value of Pi" << "		" << "Number of terms" << endl;

cout << pif(3.0) << "			" << term << endl;
cout << pif(3.1) << "			" << term << endl;
cout << pif(3.14) << "			" << term << endl;
cout << pif(3.141) << "			" << term << endl;
cout << pif(3.1415) << "			" << term << endl;
cout << pif(3.14159) << "			" << term << endl;

return 0;
}

double pif(double n)
{
double pi = 0.0; //Variable to store value of pi
int sign = 1; //Variable to store sign
bool check = false; //Variable to check value of pi

term = 0;

while (!check)
{
if (pi * 4.0 >= n) //If value of pi is greater than or equal to approx of pi
check = true; //Then exit the loop
else
{
pi *= sign * (1.0 / (1.0 + term * 2.0)); //Otherwise calculate value of pi
sign *= -1; //Change sign
++term; //And increment term
}
}

pi *= 4.0; //Perform final pi calculation after fractional sums have been determined

return pi;
}``````

Line 37: Why are you multiplying here? You should be adding or subtracting:

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

Line 33: This is an incorrect test. You need to compare the value of pi that you have calculated to the value passed to the function and see whether it is within a certain range. For example, when checking against 3.141, you are probably checking whether your calculated value of pi is within 1/1000 of 3.141. For 3.1415, you are probably checking whether your calculated values is within 1/10,000 of 3.1415. Thus you should probably round OFF the numbers before passing them to the function (i.e. pass 3.1416 rather than 3.1415 to the function).

Line 45: You should be returning the number of iterations, not pi, right? The whole idea is to find out how many iterations it takes to get within the proper tolerance.

Thanks, I didn't even realize I used multiplication in line 37. Also, how do I round off the numbers?

Hey Bro, try this-

``````#include <stdio.h>

int main()
{
double pi;
double divisor;

int term;
term = 0;
divisor = 3.0;
pi = 4.0

for ( ; 1750 > term; term++)
/* You can change the "1750" to whatever. That is just how many terms I wanted to show*/

{
divisor = ( 2 * term) + 3.0;

if (term % 2 == 0)
{
pi = pi - 4.0 / divisor
}

else
{
pi = pi + 4.0 / divisor;
}

printf("%lf term%d\n", pi, term);

}

system("pause")  /* I know that's bad but I can get it to stop automatically yet*/

return 0;
}``````

It works. I just figured it out this past Sunday.. I am new to the programming world so my "style may not be too good..

It works. I just figured it out this past Sunday.. I am new to the programming world so my "style may not be too good..

Your using C syntax in a C++ forum....

Also use [code][/code] please

@OP
rounding it off can be done by adding 0.0005 etc which which ever number of zero's it requires

Chris

Thanks :)

I still can't get it to tell me how many terms ><

It's still saying 1 term for each approximation.

I still can't get it to tell me how many terms ><

It's still saying 1 term for each approximation.

Post your updated code. We don't know what you've changed.

Be a part of the DaniWeb community

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