Final product... (dont mind the commenting; the professor wanted that stuff.)
--------------------------------------------------------
#include <stdio.h>
/* variable declarations */
double func();
double h, sum;
int i, n;
main()
{
/* prompt the user for the number of iterations (n) */
printf("How many steps? ");
scanf("%d", &n);
sum = 0; /* initialize the sum to 0 */
h = 2.0/n; /* formula for height */
/* loop to add up of the sums */
for(i = 0; i < n; i++)
{
sum += ((h/2.0)*(func(i*h) + func(i*h + h)));
}
/* print results */
printf("The integral approximation from 0 to 2 with %d steps is %f\n", n, sum);
}
/* func - this function computes the square of x
*
* inputs:
* x - the value to be squared
*
* outputs:
* the square of x
*
* side effects:
* none
*/
double func(double x)
{
return x*x;
}