Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program should run numerical integration for f(x) = e^-x^2 over the bounds of negative infinity to infinity using both simpson rule and trapezoidal rule. I need help figuring out how to pass my struct array for n to both my trap and simp function.

Thanks!

#include <stdio.h>
#include <math.h>

union Data1
{
    double x1;
};
union Data2
{
    double x2;
};
struct array
{
    int n[7];
    int sums[7];
    int sumt[7];
};

double f(double x);
double simp(int n, double x1, double x2);
double trap(int n, double x1, double x2);

int main()
{
    union Data1 data1;
    union Data2 data2;
    struct array array1 = {{1,10,25,50,100,1000,10000},{0},{0}};
    int i;
    FILE *f;

    f = fopen("data.dat", "a");

    data1.x1 = -INFINITY;
    data2.x2 = INFINITY;


    printf ("Both integration techniques will run for the following intervals: 1, 10, 25, 50, 100, 1000, 10000\n");


    for(i=0;i<7;i++)
    {
    array1.sums[i] = simp(array1.n[i],data1.x1,data2.x2);
    array1.sumt[i] = trap(array1.n[i],data1.x1,data2.x2);
    }


    printf("For the trap rule:\n");
    printf("\t\tN\t\t\tSum\n");
    for(i=0;i<7;i++)
    {
       printf("\t\t%d\t\t\t%d\n",array1.n[i],array1.sumt[i]);
    }

    printf("For the simpson rule:\n");
    printf("\t\tN\t\t\tSum\n");
    for(i=0;i<7;i++)
    {
       printf("\t\t%d\t\t\t%d\n",array1.n[i],array1.sums[i]);
    }

    fprintf(f, "For the simpson rule:\n");
    fprintf(f,"\t\tN\t\t\tSum\n");
    for(i=0;i<7;i++)
    {
       fprintf(f,"\t\t%d\t\t\t%d\n",array1.n[i],array1.sums[i]);

    }
    fprintf(f,"For the trap rule:\n");
    fprintf(f,"\t\tN\t\t\tSum\n");
    for(i=0;i<7;i++)
    {
       fprintf(f,"\t\t%d\t\t\t%d\n",array1.n[i],array1.sumt[i]);
    }
    fclose(f);

    return 0;

}

double f(double x)
{
    double y;

    y = exp(-pow(x,2));

    return y;
}

double simp(int n, double a, double b)
{
    int i;
    double dx, x, sum;

    dx = (b-a)/n;
    sum = f(a)+ f(b);
    for(i=1;i<n;i++)
    {
        x = a + dx*i;
        sum += 2*(1+i%2)*f(x);
    }
    sum*=dx/3;
    return sum;
}

double trap(int n, double a, double b)
{
    int i;
    double dx, x, sum;

    dx=(b-a)/n;
    sum = f(a)+f(b);
    for(i=1;i<n;i++)
    {
        x = a + dx*i;
        sum += 2*f(x);
    }

    sum *= dx/2;
    return sum;
}


What I have tried:

I've tried normal pointers but they did not work.
Posted
Updated 10-Apr-17 23:36pm
Comments
nv3 10-Apr-17 17:52pm    
Try

void simp (struct array* pArray, double x1, double x2);

and call it by

simp (&array1, data1.x1, data2.x2);

By the way, I don't understand what you are trying to accomplish with those unions. Just use simple double variables.
Member 13119529 10-Apr-17 18:05pm    
The unions are only there because we are required to use them for the project. I could code this whole project without structures and unions but our professor insisted on using them even though we never learned about them in class. When i add the code as you stated I get the following error: " expected 'struct array *' but argument is of type 'int'"
nv3 11-Apr-17 3:46am    
You forgot to also change the calling site and the code inside the function. The idea of passing the entire structure is that the function can do the loop over n inside and process the entire array in one call.

1 solution

actually nv3's solution is ok ,but there is a small flaw,the argument n should be added to the

function,like this,

void simp (struct array* pArray, int n,double x1, double x2);

and call it by

simp (&array1, n, data1.x1, data2.x2);

i think you will solve your problem by this way.
 
Share this answer
 
v3

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