some math problems in C

I want to calculate secant method using C language
That is a program---->
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
double fx(double x);
double x0,x1,x2,f0,f1,f2,err;
int n,i;
printf("\n\n f(x) =x*x*x-5x-7");
printf("\n\nEnter an interval [x0,x1] in"
" which root is to be found");
printf("\nx0 =");
scanf("%f",&x0); /
INTERVAL[x0,x1] is to be entered here */
printf("x1=");
scanf("%f",&x1);
printf("\n Enter the number of iterations=");
scanf("%d",&n);
printf("\npress any key for display of iterations...\n");
getchar();
i=0;
while (n > 0)
{
f0=fx(x0);
f1=fx(x1);
x2 = x1-((x1-x0)/(f1-f0))f1;
i++;
printf("\n x[%d]=%f x[%d]=%f",i,i-1,x0,i,x1);
printf("\n f[%d]=%f f[%d]=%f",i,i-1,f0,i,f1);
printf("\n x[%d]=%f",i+1,x2);
x0=x1;
x1=x2;
getchar();
}
printf("\n\nThe value of root is =%f",x2);
}
double fx(double x)
{
double f;
f=x*x*x-5
x-7;
return(f);
}

But while running not the erreo is displayed but the results displayed is
f(x) =x*x*x-5*x-7

Enter an interval [x0,x1] in which root is to be found
x0 =2.5
x1=3

Enter the number of iterations=4

press any key for display of iterations...

  x[1]=0.000000      x[0]=0.000000
  f[1]=-7.000000       f[0]=-7.000000
                           x[2]=inf

Means not able to calculate root as seen in the output it is x[1]=0...

so what will be the problem????

do you relly think you have a math-problem if you input 2.5 for x0 and you get 0 if you output x0? if x0=0 and x1=0 the function values at these points is 7 , so the math seems to be ok. to track down the bug it will be useful do reduce your program to one which inputs x0 and outputs x0 and analyze this code further.
mfgn guenter

Yah Math seems to be ok!!!
so u might know the easiet problem to solve it
Though i don't like to excuse just i am new to C and do some biological problem..
that simple things goes after me ha...
hope u can solve it!!!.:slight_smile:

hi
if you want to scan for or print a double the correct format is %lf not %f
i also think the construct

i=0;
while(n>0) {
...
i++;
...
}

will not be very useful
mfg g�nter

These:
printf("\n x[%d]=%f x[%d]=%f",i,i-1,x0,i,x1);
printf("\n f[%d]=%f f[%d]=%f",i,i-1,f0,i,f1);
need a little work as well. :wink: