C++ coding Find x, y data in a file called xydata.dat. (This is an ASCII file th
ID: 3564950 • Letter: C
Question
C++ coding
Find x, y data in a file called xydata.dat. (This is an ASCII file that can be opened with NOTEPAD or WORDPAD.) Open the file using notepad so you review the file, but do not edit the file in any manner and do not change the file name. Note the file contains two columns of data, one for x and one for y. 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.Calculate by hand the area of individual trapezoids and the total area using the first 6 x-y sets (creates 5 trapezoids). This information will be your test data.
3.Code your algorithm and do not use any concepts beyond Chapter 5 of your text book.
4.Run the program using the first 6 x-y sets. (Remember to skip the fist line of data using the ignore() function) Check to ensure that your program agrees with your calculations.
5.Rerun the program using all the data sets. Do not use a count-controlled loop, but test for end of file or state of the extraction operator. Print out the total area.
6.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
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.
Explanation / Answer
Part 1
You can create a function that, given 'x', will return 'y' using your given formula:
Then you will need to, depending on user input, calculate the area under each piece of curve, and add the results to find the total area. You will most likely need a separate loop for each method.
I left out the value for 'increment' on purpose because you should be able to figure it out. (If you wanted to cut something of 'w' width into 'j' pieces, what would the separation between each cut be?) Now instead of 'w', use 'a' and 'b' and instead of 'j', use 'n'.
Part 2
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
/* C Program for Trapezoidal Method - Source Code */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf(" Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf(" refined value of n and h are:%d %f ",n,h);
printf(" Y values ");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf(" %f ",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf(" final integration is %f",ans);
getch();
}
1 2 3 4 5 6
double f(double x) { double y = 0.; ... // do stuff to y using x here return y; } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.