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.
For starters this function declaration
does not make sense at least because it has the return type
intinstead of a floating type and a redundant number of parameters.The function should be declared at least like
And your function returns nothing though its return type is not
voidexcept this while loopwhere
totalhas the typefloatthat also does not make sense.There are other mistakes. For example in this call of
printfyou are trying to output a pointer as a floating point number.
The function can be defined the following way
And in main you can write
Here is all the code combined together
The program output is