double estimatePI( intterms) { double PI = 0; double iterations = 1; int i,terms;

14,734,526 members

See more:

Hello I have to do an assignment for C that states the following:

Write a program that will estimate the value of PI from the infinite series:

π=4/1-4/3+4/5-4/7+4/9-4/11+⋯

The program will ask the user to enter an integer and then calculate the value of PI given that many terms. For example, if the user enters 4, then the program should estimate PI using the first four terms of the sequence, and output the result of the calculation:

π=4/1-4/3+4/5-4/7≅2.895238

* Notes: You only need to output the final floating point result. Also notice that the sequence switches from adding and subtracting each term. Also be sure your program can handle the 0 case.

Your solution must include a function that returns the estimated value of PI and takes as input the number of terms used to calculated PI

double estimatePI(int terms);

Also i have to solve this using a while statement.

With the help you guys have given me I compounded the program and I am not getting any errors but whenever I run it and enter different integers, I always get 3.0000000000000000 as my answer unless i enter 0 as the integer. Not sure where in the calculations I messed up.

**What I have tried:**

Write a program that will estimate the value of PI from the infinite series:

π=4/1-4/3+4/5-4/7+4/9-4/11+⋯

The program will ask the user to enter an integer and then calculate the value of PI given that many terms. For example, if the user enters 4, then the program should estimate PI using the first four terms of the sequence, and output the result of the calculation:

π=4/1-4/3+4/5-4/7≅2.895238

* Notes: You only need to output the final floating point result. Also notice that the sequence switches from adding and subtracting each term. Also be sure your program can handle the 0 case.

Your solution must include a function that returns the estimated value of PI and takes as input the number of terms used to calculated PI

double estimatePI(int terms);

Also i have to solve this using a while statement.

With the help you guys have given me I compounded the program and I am not getting any errors but whenever I run it and enter different integers, I always get 3.0000000000000000 as my answer unless i enter 0 as the integer. Not sure where in the calculations I messed up.

```
#include <stdio.h>
#include <stdlib.h>
double estimatePI(int terms);
int main()
{
int terms;
printf("How many times would you like to calculate the value of PI by\n"
"using the infinite series: ");
scanf("%d", &terms);
printf("The value of pi = %1.16lf\n", estimatePI(terms));
return 0;
}
double estimatePI( int terms)
{
double PI = 0;
double iterations = 1;
int i;
i = 1;
while ( i <= ( terms * 2))
{
PI = PI + iterations * (4/i);
iterations = -iterations;
i += 2;
}
return PI;
}
```

Comments

You're overriding the value of your parameter, by defining a variable of the same name.

double estimatePI( intterms) { double PI = 0; double iterations = 1; int i,terms;

In addition to Solution 1, I would remove

as it is not VB.

It gives

[Update]

Re-read carefully what was written first time.

[Update]

By the way,**(4/i)** is an integer division which gives integer values.

`estimatePI = PI;`

as it is not VB.

It gives

```
while ( i <= ( terms * 2))
{
PI = PI + iterations * (4/i);
iterations = -iterations;
i += 2;
```~~estimatePI = PI;~~
}
return ~~estimate~~PI;
}

[Update]

`return ;`

Re-read carefully what was written first time.

[Update]

By the way,

v3

Comments

Wow thank you guys for the really fast answers I appreciate it. I did what you guys suggested and it gives me less errors now but I am not sure what to set my return to in the function.

Use **Improve question** to update your question.

So that everyone can pay attention to this information.

Update your code and list errors.

So that everyone can pay attention to this information.

Update your code and list errors.

Wow that was really simple. Thank you for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

Forget it... I just read it was already said below.