f(x)= x^2.

The following is my code, but I get stuck when writing for define area.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include using namespace std; double trapezoid(int n, double a, double b); // This function will take three arguments: the number of intervals, the limits and return the value of the definite integral. int main() { char yesno='y'; while(yesno=='Y' || yesno=='y') { double area, h, a, b; int n; cout<< "Enter the lower and upper limits of the interval: \n"; cin>>a>>b; cout << "Enter the number of intervals: \n"; cin>>n; cout<< "The area under the curve (intergrall) is: \n"; cin>> trapezoid>>endl; cout<<"continue? (y or n) \n"; cin>>yesno; } } double trapezoid(int n, double a, double b) { double h double h = ( ( b - a ) / n ); double y = 0; double sum = 0; for ( int i = 0; i <= n; i ++ ) { y += h; sum = sum + // dont know what have to write for f(x)= x*x; } sum = sum * ( h / 2 ); return sum; }``````
You can simply put `x * x`

or if sum is the thing you are trying to square you can simply do:
 ``123`` ``````sum *= sum; //or sum = sum * sum;``````

Also it would be impossible to input into a newline. (line 23 remove endl)
sum is not the thing I try to square.
I change x*x to x * x and take out line 23 but the program still some error that I cant run.
 sum is not the thing I try to square.

So then what are you trying to square?

 but the program still some error that I cant run.

It helps if you tell us what the error is.
well um you are supposed to replace x with some variable otherwise that would be like trying to do `??? * ???` What exactly are you trying to square? Also I didn't mean remove line 23 completely I meant remove the endl on line 23.
missmango wrote:

`I get stuck when writing for define area.`

Indeed, you are wrong when define area of an elementary trapezoid. The area is:

area = medium_lineLength * trapezoid_height

so please calculate the two values and all wil be OK.

Last edited on
As an example (you have to extract the trapezoid function!)

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include int main(){ double a, b, h, sum = 0.; double x1, x2, median; double area; int n; std::cout << "Enter the ends of the interval [a, b] : \n"; std::cout << "a = "; std::cin >> a; std::cout << "b = "; std::cin >> b; if(b < a) return 1; std::cout << "Number of subintervals of [a, b]? "; std::cin >> n; // The length of a subinterval h = (b - a) / n; for(int i = 0; i < n; ++i){ x1 = a + i * h; // abscissa of the left end x2 = x1 + h; // abscissa of the right end // For the function f(x) = x^2 we have median = (x1 * x1 + x2 * x2) * .5; // The area of each trapezoid area = median * h; // The area under the curve sum += area; } std::cout << "Approx. area = " << sum << '\n'; std::cout << "Exact area = " << (b * b * b - a * a * a) / 3. << '\n'; return 0; }`````` ```Example 1 ********** Enter the ends of the interval [a, b] : a = 0 b = 4 Number of subintervals of [a, b]? 10 Approx. area = 21.44 Exact area = 21.3333 Example 2 ********** Enter the ends of the interval [a, b] : a = 0 b = 4 Number of subintervals of [a, b]? 100 Approx. area = 21.3344 Exact area = 21.3333 Example 3 ********** Enter the ends of the interval [a, b] : a = 0 b = 4 Number of subintervals of [a, b]? 1000 Approx. area = 21.3333 Exact area = 21.3333```
Topic archived. No new replies allowed.