Home Contents



/*      Introduction to program:
        Program - trapez2.c.
        Computes an approximation of the integral of x^2 between two real number bounds.
        Uses functions.
        Francis O'Donovan CK403 97611701 6-2-97. */

/*      Files to be #included. */

        #include <stdio.h>

/*      #definitions. */

        #define LENGTH 6
        /*      Defines the dimension of the array.
        This is the number of intervals plus one. */

/*****************************************************/

/*      Function: main(). */

main()
{
        /* Function definitions. */

        double DivideInterval();
        void InitializeArray();
        double ApproxArea();
        double ExactArea();
 
        /* Variable definitions. */

        double a, b, h, area, exact_area;
                /*   a and b are bounds for integral.
                h is interval width.
                area is the approximate area.
                exact_area is the exact solution of the integral.
                Use 'double' to allow high precision decimals.     */
 
                double x[LENGTH];
                /*   array for sub-interval values. */
 
        /* Print introduction on screen. */

        printf( "Program: trapez2.c.\n\n" );
        printf( "Computes an approximation of the integral of x^2 between ");
        printf( "two real number bounds.\n" );
        printf( "Uses functions.\n\n" );
        printf( "Francis O'Donovan 6-2-97\n\n" );

        /* Get the user to input the two bounds */

        printf( "Please type in the two real number bounds, pressing " );
        printf( "return after each one.\n" );
        printf( "\na = ");
        scanf( "%lf", &a );
        printf( "\nb = ");
        scanf( "%lf", &b );

        /* Calculate the interval width. */

        h = DivideInterval(a, b); /* Passes the values of a and b as arguements.*/
 
        /* Fill in the x[i] array values with appropriate values. */

        InitializeArray( a, x, h ); /* Passes the value of a and h and
                                        the address of x[0] as arguements. */
 
        /* Calculate the approximate area using the trapezoidal formula. */
 
        area = ApproxArea( x, h);    /* Passes the value of h and the
                                        address of x[0] as arguements. */
 
        /* Output the approximate area. */

        printf( "\nThe approximate area is %.3f ", area );
        printf( "(to three decimal places).\n\n" );

        /* Output the exact area, calculated using the integral. */
 
        exact_area = ExactArea( a, b);  /* Passes the value of a and
                                                b as arguements. */

        printf( "The exact area is %.3f ", exact_area );
        printf( "(to three decimal places).\n" );
 

}

/* ************************************************ */

/*      Function: DivideInterval().
        Purpose: Calculates subinterval width between two numbers,
                when there is five subintervals.
        Arguements: Two doubles.
        Returns: double.
*/

double DivideInterval(a, b)
        double a, b;
{
        /* Initialize local variables. */

        double h;       /* Interval width. */
 
        /* Calculate interval width using formula. */

        h = (b-a)/(LENGTH-1);

        /* Return value of h. */

        return(h);
}

/******************************************************/

/*      Function: InitializeArray().
        Purpose: Fills in array with appropriate values.
        Arguements: Pointer to array and two doubles.
        Returns: void.
*/

void InitializeArray( a, x, h)
        double a, *x, h;
{
        /* Initialize local variables. */
        int i;          /* Counter. */

        /* x points to x[0], the first element of the
        array in main(). */

        for( i = 0; i < LENGTH; i++ )
        {
                *(x+i) = a + (i*h);
                /*      x[0] = a, x[1] = a+h, x[2] = a+2h, etc. */
        }

}
/*****************************************************************/

/*      Function: ApproxArea().
        Purpose: Calculates approximate area using trapezoidal formula.
        Arguements: Double and pointer to array.
        Returns: double.
*/

double ApproxArea( h, x)
        double h, *x;
{
        /* Initialize local varibles. */
        int i;
        double area = 0;

        /* x points to x[0], the first element of the
        array in main(). */

        for( i = 0; i < LENGTH-1; i++ )
        {
                area += (h/2)*(x[i]*x[i] + x[i+1]*x[i+1] );
        }

        /* Calculate |area| as area is always positive. */

        if ( area < 0 )
                area = area * (-1);

        /* Return value of area. */
 
        return( area );

}
/*****************************************************************/

/*      Function: ExactArea().
        Purpose: Calculates exact area using integral formula.
        Arguements: Two doubles.
        Returns: double.
*/

double ExactArea( a, b)
        double a, b;
{
        /* Initialize local varibles. */
        double exact_area;

        /* Calculate exact area from the formula (((b*b*b)/3)-((a*a*a)/3)). */
        exact_area = (((b*b*b)/3)-((a*a*a)/3));
 
        /* Calculate |exact_area|. */
        if ( exact_area < 0 )
                exact_area = exact_area * (-1) ;
 
        /* Return value of exact_area. */
 
        return( exact_area );

}
/*****************************************************************/
 


Home Contents



© Francis O'Donovan 1999.