Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C
#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;
    }
Posted
Updated 15-Mar-20 9:10am
v4
Comments
Patrice T 15-Mar-20 14:31pm    
Now the list of errors.
Nelek 15-Mar-20 15:01pm    
4/i might still be integer division. You might want to use 4.0/i to be sure

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

You're overriding the value of your parameter, by defining a variable of the same name.
double estimatePI( int terms)
{
double PI = 0;
double iterations = 1;
int i, terms;
 
Share this answer
 
In addition to Solution 1, I would remove
C++
estimatePI = PI;

as it is not VB.
It gives
C++
    while ( i <= ( terms * 2))
    {
        PI = PI + iterations * (4/i);
        iterations = -iterations;
        i += 2;
        estimatePI = PI;
    }
return estimatePI;
}


[Update]
C++
return ;

Re-read carefully what was written first time.

[Update]
By the way, (4/i) is an integer division which gives integer values.
 
Share this answer
 
v3
Comments
Member 14770192 15-Mar-20 14:05pm    
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.
Patrice T 15-Mar-20 14:08pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Update your code and list errors.
Member 14770192 15-Mar-20 14:50pm    
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