find the sum under the curve with a file

Hello
I have to find the sum under a curve using trapezoids and all the points of the curve have been put in a file but I do not know how to actually write it into codes.

Here's what I have so far:
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
  #include <fstream>
#include <iostream>

double x1=0;
double x2=0;
double y1=0;
double y2=0;
double n;
double sum;
double area;

using namespace std;

int main()
{
	ifstream infile;
	infile.open("xydata.dat");
	infile.ignore(1, '\n') ;
	while(!infile.eof())
	{
		infile>>n;
		if(n>=20.00&&n<=20.50)
		{
			if(x1!=0)
			{
				x2=x1;
			}
			else
			{
				x1=n;
			}
		}
		else
		{
			if(y1!=0)
			{
				y2=y1;
			}
			else
			{
				y1=n;
			}
		}

		area = (x1+x2)*(y2-y1)/2;

	}

	cout<<"The sum of all are under the curve is "<<sum<<"."<<endl;

	return 0;
}

and the file is here:
xydata.dat

x values y values
20.00 0
20.02 15
20.04 27
20.06 39
20.08 54
20.10 65
20.12 75
20.14 84
20.16 93
20.18 101
20.20 108
20.22 113
20.24 116
20.26 115
20.28 112
20.30 107
20.32 100
20.34 92
20.36 83
20.38 74
20.40 64
20.42 53
20.44 39
20.46 27
20.48 15
20.50 0
Last edited on
Topic archived. No new replies allowed.