#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
//Begin by defining the functions that are to be called in the main program
double f (double x) //Function 1 Finding 1/x, the integrand, this shall be called f
{ //Function 1 Body
double y;
y=1.0/x;
return y;
} //End of Function 1
double trap (int n, double a, double b) //Function 2 Calculation of the areas of the strips, called trap
{
double d, h=0, area;
d=(b-a)/n; //d=Strip Width, "a" and "b" are the upper and lower bounds of the integral
for (int i=1, i<=(n-1), i++)
{
y+=(f((a+(i*d)))); //This sums the y values from 1 to (n-1)
}
area=(d*y) + (d/2.0)*(f(a)+f(b)); //Adds the final part of the trapezium rule to the number returned in the for loop above to complete the area
return area; //End of Function 2
}
int main() //Beginning of the Main Function
{
double a, b, TrapArea;
int n;
cout<<"This will evaluate the integral of the function f(x)=1/x" <<endl;
cout<<"Please enter a value for the lower bound of the integral"<<endl;
cin>>a; //Value inputted for a
cout<<"Please enter a value for the upper bound"<<endl;
cin>>b; //Value inputted for b
cout<<"Please enter an integer value for the number of strips"<<endl;
cin>>n; //Value inputted for n
TrapArea=trap(n,a,b);
cout<<"The area calculated using the trapezium rule is apporixmately"<<TrapArea<<endl;
}
return 0;
Been trying to create a program to evaluate an integral through the trapezium rule. And I've got these errors! Any help would be much appreciated, thanks
Errors:
error C2143: syntax error : missing ',' before '<='
error C2086: 'int i' : redefinition
see declaration of 'i'
error C2143: syntax error : missing ';' before '{'
error C2065: 'y' : undeclared identifier
error C2065: 'y' : undeclared identifier
error C2059: syntax error : 'return'