#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'

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++)   // changed commas to semicolons
	{
	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
	}

You defined y in function f, but not function trap, nor did you define y globally. So function trap doesn't know what y is when you use it inside that function (line 7 above). In your for loop, you need to change the commas to semicolons (changed in line 5 above, see comment). Also, your "return 0" command is after the brackets that end your main function. It needs to be inside main.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.