# Need help with an Algebra calculation

• 02-08-2002
Unregistered
Need help with an Algebra calculation
I have written a program to calculate the area under a curve, using the area of a trapezium rule. As there are two values that vary and not one, the integration rule wont work. So I want to work out each trapezium's area and then sum them.

I have set up two arrays, x and y. The user enters the values into x and then y. Now i need to make the calculation using the following rule, using i as the current value:

((x[i+1]-x[i])*(y[i]+y[i+1]))/2

This gives the area for each trapezium. Then I need to sum them all together to give a total area and thus the area under a curve.

P.S. the area of a trapezium:

Area = (interval width * positive difference in heights)/2

Can anyone help me with the for statements to do this please?
• 02-08-2002
Unregistered
EDIT:

the area of a trapezium SHOULD read:

Area = (width * (height left + height right))/2
• 02-08-2002
QuestionC
You want to add the values up while you run along the for loop. It is very inefficient to actually store the values and then add them up later.
Code:

int numValues = the number of values
int i;
float area = 0;

// numValues - 1, for example, the points (0, 0), (1, 3), (3, 1)
//  represent two trapeziums.
for (i = 0; i < numValues - 1; i++)
{
area += ((x[i+1]-x[i])*(y[i]+y[i+1]))/2
}
return area;