Multiple Integration Methods

So, for an assignment in C++, I need to write a program which calculates the area under the curve of a certain input for a, b, and n values. I have the math formula down, but the assignment has a catch. I need to define the f() function separate from the main. I thought I had this written out correctly, and the compiler found no problems, but when I try running it, I get a message saying the code was not compiled, even though I did compile it. Does anyone know what the problem here is?

Update:
I've solved the problem with the compiler, but now I have a new problem. When I type in the values the trapazoidal method's output gets more out of sync with the other answers. Do I have the formula wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdlib.h>

double f(double);
using namespace std;


int main()
{
    ofstream output("c:result.dat");
    double x, a, b, n, i, delx, area;
    cout<<"Enter values for a, b, and n.";
    cin>>a>>b>>n;
    area=0;
    delx=(b-a)/n;
    
    //Lower Limit
    for(i=0; i<n; ++i)
    {
             area=area+f(a+i*delx)*delx;
    }
    cout<<"The area under the curve is "<<area<<"."<<endl;
    output<<"The area under the curve is "<<area<<"."<<endl;
    area=0;
    
    //Upper Limit
    for(i=1; i<=n; ++i)
    {
             area=area+f(a+i*delx)*delx;
    }
    cout<<"The area under the curve is "<<area<<"."<<endl;
    output<<"The area under the curve is "<<area<<"."<<endl;
    area=0;
    
    //Midpoint Method
    for(i=0; i<n; ++i)
    {
             area=area+f((a+0.5*delx)+i*delx)*delx;
    }
    cout<<"The area under the curve is "<<area<<"."<<endl;
    output<<"The area under the curve is "<<area<<"."<<endl;
    area=0;
    
    //Trapazoidal Method
    for(i=0; i<n; ++i)
    {
             area=area+f((a+i*delx)+(a+(i+1))*delx)*0.5*delx;
    }
    cout<<"The area under the curve is "<<area<<"."<<endl;
    output<<"The area under the curve is "<<area<<"."<<endl;
    return 0;
}

double f(double x)
{
       return (0.6*pow(x,2)+2*x+3);
}
Last edited on
Compiles for me.

 $ g++ int.cc
$ ./a.out 
Enter values for a, b, and n.1 2 10
The area under the curve is 7.211.
The area under the curve is 7.591.
The area under the curve is 7.3995.

Did you figure this out?
Topic archived. No new replies allowed.