#include <stdio.h>
#include <math.h>
double f(double x)
{
return (1/(x*sin(x*x)));
}
int main()
{
int i=1, num=20, N;
double b, APP, TOL[num], a[num], h[num], FA[num], FB[num], FC[num], S[num], S1, S2, L[num], FD, FE, v[num];
printf("Left Endpoint: ");
scanf("%lf", &a);
printf("Right Endpoint: ");
scanf("%lf", &b);
printf("Desired Tolerence: ");
scanf ("%lf", &TOL);
printf("Number of Levels: ");
scanf("%d", &N);
APP = 0;
TOL[num] = 10.0*TOL;
a[num] = a;
h[num] = (b-a)/2;
FA[num] = f(a);
FC[num] = f(a+h[num]);
FB[num] = f(b);
S[num] = h[num]*(FA[num]+4*FC[num]+FB[num])/3;
L[num] = 1;
while (i>0)
{
FD = f(a[num] + h[num]/2);
FE = f(a[num] + 3*h[num]/2);
S1 = h[num]*(FA[num]+4*FD+FC[num])/6;
S2 = h[num]*(FC[num]+4*FE+FB[num])/6;
v[1] = a[num];
v[2] = FA[num];
v[3] = FC[num];
v[4] = FB[num];
v[5] = h[num];
v[6] = TOL[num];
v[7] = S[num];
v[8] = L[num];
i=i-1;
if (abs(S1 + S2 - v[7])<v[6])
{
APP = APP + (S1 + S2);
}
else
{
if (v[8]>=N)
{
printf("Level Exceeded");
return;
}
else
{
i++;
a[num] = v[1] + v[5];
FA[num] = v[3];
FC[num] = FE;
FB[num] = v[4];
h[num] = v[5]/2;
TOL[num] = v[6]/2;
S[num] = S2;
L[num] = v[8] + 1;
i++;
a[num] = v[1];
FA[num] = v[2];
FC[num] = FD;
FB[num] = v[3];
h[num] = h[num-1];
TOL[num] = TOL[num-1];
S[num] = S1;
L[num] = L[num-1];
}
}
}
printf("The Integral Approximation is %lf." , APP);
}
Build Messages:
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c||In function 'main':|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|34|error: invalid operands to binary * (have 'double' and 'double *')|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|35|error: incompatible types when assigning to type 'double' from type 'double *'|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|36|error: invalid operands to binary - (have 'double' and 'double *')|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|38|error: incompatible type for argument 1 of 'f'|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|10|note: expected 'double' but argument is of type 'double *'|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|39|error: invalid operands to binary + (have 'double *' and 'double')|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 0 seconds) ===|
I have not used arrays before, which is probably a big part of my problem. I believe I have at least come a long way since I did not know any programming before this semester. Thanks for your help.
[edit]
I edited my program as you described:
#include <stdio.h>
#include <math.h>
#define num 20
double f(double x)
{
return (1/(x*sin(x*x)));
}
int main()
{
int i=1, N;
double b, APP, TOL[num], a[num], h[num], FA[num], FB[num], FC[num], S[num], S1, S2, L[num], FD, FE, v[num];
printf("Left Endpoint: ");
scanf("%lf", &a);
printf("Right Endpoint: ");
scanf("%lf", &b);
printf("Desired Tolerence: ");
scanf ("%lf", &TOL);
printf("Number of Levels: ");
scanf("%d", &N);
APP = 0;
TOL[num] = 10.0*TOL;
a[num] = a;
h[num] = (b-a)/2;
FA[num] = f(a);
FC[num] = f(a+h[num]);
FB[num] = f(b);
S[num] = h[num]*(FA[num]+4*FC[num]+FB[num])/3; L[num] = 1;
while (i>0)
{
FD = f(a[num] + h[num]/2);
FE = f(a[num] + 3*h[num]/2);
S1 = h[num]*(FA[num]+4*FD+FC[num])/6;
S2 = h[num]*(FC[num]+4*FE+FB[num])/6;
v[1] = a[num];
v[2] = FA[num];
v[3] = FC[num];
v[4] = FB[num];
v[5] = h[num];
v[6] = TOL[num];
v[7] = S[num];
v[8] = L[num];
i=i-1;
if (abs(S1 + S2 - v[7])<v[6])>=N)
{
printf("Level Exceeded");
return;
}
else
{
i++;
a[num] = v[1] + v[5];
FA[num] = v[3];
FC[num] = FE;
FB[num] = v[4];
h[num] = v[5]/2;
TOL[num] = v[6]/2;
S[num] = S2;
L[num] = v[8] + 1;
i++;
a[num] = v[1];
FA[num] = v[2];
FC[num] = FD;
FB[num] = v[3];
h[num] = h[num-1];
TOL[num] = TOL[num-1];
S[num] = S1;
L[num] = L[num-1];
}
}
}
printf("The Integral Approximation is %lf." , APP);
}
Still Obtaining:
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c||In function 'main':|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|35|error: invalid operands to binary * (have 'double' and 'double *')|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|36|error: incompatible types when assigning to type 'double' from type 'double *'|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|37|error: invalid operands to binary - (have 'double' and 'double *')|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|39|error: incompatible type for argument 1 of 'f'|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|11|note: expected 'double' but argument is of type 'double *'|
C:\Users\Mal\Desktop\MTH 415\Adaptive Quadrature 2.c|40|error: invalid operands to binary + (have 'double *' and 'double')|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 0 seconds) ===|</math.h></stdio.h>
[/edit]