Simpson rule problem

I have to develop a program to implement the Simpson rule. I have been working on it all night and I can't get it right. Below is my most recent attempt.

#include <cstdlib>
#include <iostream>


using namespace std;
double function(double x) {
double f=x*x*x;
return f;
}


double simpson(double a, double b, double n) {
double h=(b-a)/(n-1);
double s=function(a);

for(int k=1; k<n; k+2) {
double x = a + h*(k);
s=s+4.0*function(x);
}
for(int k=2; k<(n-1); k+2) {
double y = a+h*(k);
s=s+2.0*function(y);
}
s=s+function(b);
double integral =(h/3.0)*s;
return integral;
}

int main(int argc, char *argv[])
{
cout<<simpson(1.0,5.0,7.0)<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}

The output is nothing. It was a huge negative number but now I just get nothing. :(

Any help would be appreciated.
george x
1
2
3
4
for(int k=2; k<(n-1); k+2) {
double y = a+h*(k);
s=s+2.0*function(y);
}


k+2 doesnt increase k. Just use k+=2
Topic archived. No new replies allowed.