Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include <stdio.h>
#include <math.h>

double f(double x)
{
    return (1/(x*sin(x*x)));
    //return (2*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;    //Simpsons approximation
    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]
C++
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)));
    //return (2*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;    //Simpsons approximation
    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]
Posted
Updated 26-Mar-14 7:16am
v2

1 solution

C++
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];

You cannot use a variable in this case to declare an array, you must use a constant value, like:
C++
#define NUM 20
    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];
 
Share this answer
 
Comments
MalDrHoop 26-Mar-14 13:13pm    
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)));
//return (2*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; //Simpsons approximation
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);

}


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) ===|
Richard MacCutchan 26-Mar-14 13:23pm    
FC[num] = f(a+h[num]);
You cannot add an array (a) to a variable (h[num]). You also have the problem that you are using num as your array index in every reference, which is invalid and will give you erroneous results or crash your program. I suggest going back to your C reference documentation and study array accessing. And your loop index (i) seems to be going in both directions, incrementing and decrementing.
MalDrHoop 26-Mar-14 15:46pm    
I did as indicated in the algorithm... I do not know how to fix this.
Richard MacCutchan 27-Mar-14 4:46am    
I suggest you spend more time learning the C language and practicing with simple examples, before trying something which contains concepts that you do not understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900