Please give code in the simplest form with using the x and y values from the \'x
ID: 3565026 • Letter: P
Question
Please give code in the simplest form with using the x and y values from the 'xydata' file using #include <fstream> and 'infile.open' without using functions. Using while loop.
Rather than using a function to calculate y at various points, the y values have already be measured and recorded for you. Consecutive pairs of x-y data should be used to designate the trapezoids, e.g. the first trapezoid would be defined by x1, y1, x2, and y2; the second trapezoid would be defined as x2, y2, x3, and y3; the third trapezoid by x3, y3, x4, and y4, etc.
1.For this assignment you are to write a computer program(C++) to calculate the area under a curve for x-y data contained in a file. Develop the algorithm necessary to calculate the area under curve that is represented by this data.
2. Do not use arrays or declare more than two variables for x-values and two for y-values.
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
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main(){
ifstream infile;
infile.open("xydata.dat");
double x1,x2,y1,y2;
infile >> x1;
infile >> y1;
while(!infile.eof()){
infile >> x2;
infile >> y2;
double area = ((x2 - x1)*(y2 + y1))/2;
cout << area << endl;
x1 = x2;
y1 = y2;
}
return 0;
}