help needed calling midinf subroutine


Alex_nr
03-12-2007, 01:22 PM
I have written a program that calls the midinf subroutine but I am unable to figure out the change I need to do as I am getting strange errors. Also there's a change of variable in the integration and I have implemented that as well.Can anyone figure out my mistakes in this program? The declarations and function calling is implemented in the same way I did for other subroutines that worked well but this is getting stuck....Please replu as early as possible.

float u,v,w,y,t;
int n0;
printf("enter the values of v");
scanf("%f", &v);
printf("enter the values of w");
scanf("%f", &w);
printf("enter the values of n0");
scanf("%d", &n0);
func = eqnx;
((*func)(1.0/(x))/((x)*(x));
t=midinf(eqnx,v,w,n0);
printf("t=%f", t);
}
float eqnx(float y)
{
return(y*y);
}
float midinf(float (*func)(float), float aa, float bb, int n)
{
float x,tnm,sum,del,ddel,b,a;
static float s;
int it,j;
b=1.0/aa;
a=1.0/bb;
if (n == 1) {
return (s=(b-a)*FUNC(0.5*(a+b)));
} else {
for(it=1,j=1;j<n-1;j++) it *= 3;
tnm=it;
del=(b-a)/(3.0*tnm);
ddel=del+del;
x=a+0.5*del;
sum=0.0;
for (j=1;j<=it;j++) {
sum += FUNC(x);
x += ddel;
sum += FUNC(x);
x += del;
}
return (s=(s+(b-a)*sum/tnm)/3.0);
}
}

Thank you,
Alex

Alex_nr
03-13-2007, 05:39 AM
I have edited the program to this though my previous doubt is still unclear so I would request an answer on that as well. The code below is compiling but chenging n0 changes the result to a large extent, which isn't possible theoretically. Any comment would be highly appreciated.To make it clear I am copying the error , please see at the end of the program.

#define FUNC(x) ((*func)(1.0/(x))/((x)*(x)))
float eqnx(float y);
float x;
float midinf(float (*func)(float), float v, float w, int n0);
main()

{
float u,v,w,y,t;
int n0;
printf("enter the values of v");
scanf("%f", &v);
printf("enter the values of w");
scanf("%f", &w);
printf("enter the values of n0");
scanf("%d", &n0);
t=midinf(eqnx,v,w,n0);
printf("t=%f", t);
}
float eqnx(float y)
{
return(y*y);
}
float midinf(float (*func)(float), float aa, float bb, int n)
{
float x,tnm,sum,del,ddel,b,a;
static float s;
int it,j;
b=1.0/aa;
a=1.0/bb;
if (n == 1) {
return (s=(b-a)*FUNC(0.5*(a+b)));
} else {
for(it=1,j=1;j<n-1;j++) it *= 3;
tnm=it;
del=(b-a)/(3.0*tnm);
ddel=del+del;
x=a+0.5*del;
sum=0.0;
for (j=1;j<=it;j++) {
sum += FUNC(x);
x += ddel;
sum += FUNC(x);
x += del;
}
return (s=(s+(b-a)*sum/tnm)/3.0);
}
}

~
$ gcc midinf2.c -o midinf2.exe
midinf2.c:47:2: warning: no newline at end of file

~
$ ./midinf2.exe
enter the values of v1
enter the values of w4
enter the values of n010
t=13.998841
~
$ ./midinf2.exe
enter the values of v1
enter the values of w4
enter the values of n020
t=2.771515
~
$

Thanks and regards,
Alex