Home Contents


/*      Introduction to program:
        Program - trapez.c.
        Computes an approximation of the integral of x^2 between two real number bounds.
        Francis O'Donovan 15-12-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()
        {
                /* Variable defintions. */

                        int i;
                        /*   counter for 'for loop'. */
 
                        double a, b, h, area = 0, 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: trapez.c.\n\n" );
                        printf( "Computes an approximation of the integral of x^2 between ");
                        printf( "two real number bounds.\n\n" );
                        printf( "Francis O'Donovan 15-12-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 from the formula h = (b-a)/(LENGTH-1). */

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

                /* Fill in the x[i] array values with appropriate values. */

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

                /* Calculate the approximate area using the trapezoidal formula. */
 
                        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);

                /* Output the approximate area. */

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

                /* Output the exact area from the formula (((b*b*b)/3)-((a*a*a)/3)) */

                        exact_area = (((b*b*b)/3)-((a*a*a)/3));
                        if ( exact_area < 0 )
                                exact_area = exact_area * (-1) ;        /* |exact_area| once more */
                        printf( "The exact area is %.3f ", exact_area );
                        printf( "(to three decimal places).\n" );

 
        }


Home Contents



© Francis O'Donovan 1999.