All pastes #2057284 Raw Edit

TestSimpson

public cpp v1 · immutable
#2057284 ·published 2011-05-12 09:40 UTC
rendered paste body
#include <iostream>double f(double x) {	// implement the function here	return double(1)/(1+x);}double simpson(double a, double b, int n, double _f(double)) {	int m = n/2;	double x = a, sigma1 = 0, sigma2 = 0;	double h = (b-a)/(2*m);	for (int i = 1; i < 2*m; i++) {		x = a + i*h;		if (i%2 != 0)			sigma1 += _f(x);		else			sigma2 += _f(x);	}	return (h/3)*(_f(a) + _f(b) + 4*sigma1 + 2*sigma2);}int main() {	std::cout << simpson(0, 1, 10, f);	return 0;}