Hi all

I just finished my simpson 3/8 rule program. But I'd like to modify it.

The program should stop when a given precision is reached...Without the

if clause the programm calculates the value very good..but how can I maked it stop, when the precision of 10^6 is reached?

Code:

#include <stdio.h>
#include <math.h>
double function(double x);
main()
{
printf("-----The Program calculates the integral with 'Simpson 3/8 rule'-----\n");
// Interval: [a,b] h=(b-a)/3m
// e.g choose m = 100 so I have 300 strips
int m = 50000;
int a = -1;
int b = 1;
double h = (double)(b-a)/(3*m);
double funArray[3*m];
int funArraySize = sizeof(funArray)/sizeof(double);
// funArray index
int i = 0;
// function values
double k;
for(k=-1;k<=1;k=k+h)
{
funArray[i] = function(k);
i++;
}
double tmp;
int r;
double simpsonSum = 0;
for(r=1;r<=m;r++)
{
simpsonSum = simpsonSum + funArray[3*r-3]+3*funArray[3*r-2]+3*funArray[3*r-1] + funArray[3*r];
// if clause which does not work ..
if(h*(0.375)*abs(tmp-simpsonSum)<0.000001)
break;
tmp = simpsonSum;
}
simpsonSum = (double)simpsonSum*h*(0.375);
printf("%0.10lf", simpsonSum);
return 0;
}
double function(double x)
{
return (log(1+pow(x,2))*sqrt(cos(x)));
}

thx for helping me!

Matts