/* * file: a1.c * Solution to assignment 1, CSC 270, Fall 99 * Program extends the rectangle, midpoint and trapezoid rules * to approximate 3D integration. */ #include #include "func.h" /* * Rectangle rule */ float rectangle (float minx, float maxx, float miny, float maxy) { return (maxx - minx) * (maxy - miny) * func(minx, miny); } /* * Midpoint rule */ float midpoint (float minx, float maxx, float miny, float maxy) { return (maxx - minx) * (maxy - miny) * func((minx + maxx) / 2, (miny + maxy) / 2); } /* * Trapezoid rule */ float trapezoid (float minx, float maxx, float miny, float maxy) { return ((maxx - minx) * (maxy - miny) * (func(minx, miny) + func(maxx, miny)) / 2); } /* * Composite rule calculates the size of the rectangular intervals. * Sums the volume of the individual intervals. */ float composite (float ff (float, float, float, float), float minx, float maxx, float miny, float maxy, int nxintervals, int nyintervals) { float xwidth = (maxx - minx) / (float) nxintervals; float ywidth = (maxy - miny) / (float) nyintervals; float volume = 0.0; int xcount; int ycount; for (xcount = 0; xcount < nxintervals; xcount++) for (ycount = 0; ycount < nyintervals; ycount++) { volume += (*ff) (minx + xwidth * xcount, minx + xwidth * (xcount + 1), miny + ywidth * ycount, miny + ywidth * (ycount + 1)); } return volume; } /* * Reads the integral range and number of subintervals. * Reports the results for the three different rules. */ main () { float xmin, xmax, ymin, ymax; float volumeRectangle, volumeMidpoint, volumeTrapezoid; int xIntervals, yIntervals; printf ("input integral range for x values, minimum x value, maximum x value\n"); scanf ("%f%f", &xmin, &xmax); printf ("input integral range for y values, minimum y value, maximum y value\n"); scanf ("%f%f", &ymin, &ymax); printf ("input the number of intervals in the x direction\n"); scanf ("%d", &xIntervals); printf ("input the number of intervals in the y direction\n"); scanf ("%d", &yIntervals); volumeRectangle = composite (rectangle, xmin, xmax, ymin, ymax, xIntervals, yIntervals); volumeMidpoint = composite (midpoint, xmin, xmax, ymin, ymax, xIntervals, yIntervals); volumeTrapezoid = composite (trapezoid, xmin, xmax, ymin, ymax, xIntervals, yIntervals); printf ("rectangle %f\n", volumeRectangle); printf ("midpoint %f\n", volumeMidpoint); printf ("trapezoid %f\n", volumeTrapezoid); }