Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef double (* function) (double x);
- double f(double x){
- return x;
- }
- double X(int i, double a, double h){
- return a + i * h;
- }
- double Trapezoidal(double a, double b, int N, function f){
- double h = (b - a) / double(N);
- double S = 0;
- for(int i = 1; i <= N; ++i){
- S += h * (f(X(i - 1, a, h)) + f(X(i, a, h))) / 2.;
- }
- return S;
- }
- int main(){
- double a, b;
- cout << "Enter a, b:\n";
- cin >> a >> b;
- cout << "Enter the number of segments:\n";
- int N;
- cin >> N;
- cout << Trapezoidal(a, b, N, f);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement