Search code examples
cfor-loopfloating-pointprintffunction-definition

Trouble with getting a function to calculate and loop correctly in C


I am needing to make a program to take user input of iterations to the formula : PI=4(1/1 -(1/3)+(1/5) ... repeating a number of times. MY problems so far is that it is not taking my input accurately.

Bascially my approach was to try and seperate the numerator and denominator wth x in the denominator and try and get it to add 2 but I haven't even figured out the - + rotation yet since I wanted to make sure what I had was working first.

My first problem was that it was getting random numbers but then I changed the variable type to float and that solved that problem but now everything is straight 0.

int pi_formula(float numerator,int x,int n)
{

    numerator=1.0;
    x=1;
    float fraction= numerator/x;
    float total;
    
    printf("%lf",&fraction);

    for (int i = 1; i <= n; i++) // loop goes for amount of user input times
        
 
        do
    {
        
        fraction; 
        return(total);
        x+= 2;              
     } while (1);
     
      printf("Estimated PI is : %f",&total);



}

When I run this code and the line

printf("%lf",&fraction); 

will return 0.000000.

I can't tell if I even have the loop written properly because I can't seem to get my input to be rcognized.

Here are the prototypes and the main for reference.

int pi_formula(float numerator,int x,int n);


int main ()
{

// Problem 2
    float numerator;
    int x;
    int n;
    printf("ESTIMATE PI\n");
    printf("Enter number of iterations:");
    scanf("%i",&n);
    pi_formula(numerator,x,n);

     




system("pause");
}



I had tried setting up printf calls to check the inputs expecting to see what I input but that also comes up 0.


Solution

  • For starters this function declaration

    int pi_formula(float numerator,int x,int n)
    

    does not make sense at least because it has the return type int instead of a floating type and a redundant number of parameters.

    The function should be declared at least like

    double pi_formula( unsigned int n );
    

    And your function returns nothing though its return type is not void except this while loop

        do
    {
        
        fraction; 
        return(total);
        x+= 2;              
     } while (1);
    

    where total has the type float that also does not make sense.

    There are other mistakes. For example in this call of printf

    printf("Estimated PI is : %f",&total);
    

    you are trying to output a pointer as a floating point number.

    The function can be defined the following way

     double pi_formula( unsigned int n )
     {
         double pi = 0.0;
    
         for ( unsigned int i = 0; i < n; i++ )
         {
             if ( i % 2 == 0 )
             {
                 pi += 1.0 / ( 2 * i + 1 );
             }
             else
             {
                 pi -= 1.0 / ( 2 * i + 1 );
             }  
         }
    
         return 4 * pi;
    }  
    

    And in main you can write

    int main( void )
    {
        puts( "ESTIMATE PI" );
    
        printf( "Enter number of iterations: " );
      
        unsigned int n = 0;
    
        if ( scanf( "%u", &n ) == 1 )
        {
             printf( "Estimated PI is : %f\n", pi_formula( n ) );
        }
    }
    

    Here is all the code combined together

    #include <stdio.h>
    
    double pi_formula( unsigned int n )
    {
        double pi = 0.0;
    
        for (unsigned int i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
                pi += 1.0 / ( 2 * i + 1 );
            }
            else
            {
                pi -= 1.0 / ( 2 * i + 1 );
            }
        }
    
        return 4 * pi;
    }
    
    int main( void )
    {
        puts( "ESTIMATE PI" );
    
        printf( "Enter number of iterations: " );
    
        unsigned int n = 0;
    
        if (scanf( "%u", &n ) == 1)
        {
            printf( "Estimated PI is : %f\n", pi_formula( n ) );
        }
    }
    

    The program output is

    ESTIMATE PI
    Enter number of iterations: 9999
    Estimated PI is : 3.141693