Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have written the following code to perform a numerical integration of the data points. This integration is supposed to compute the area of the

I have written the following code to perform a numerical integration of the data points. This integration is supposed to compute the area of the curve of the data points. The code compiles, runs, and gives an output, but the output is a little odd and runs as an infinite loop. Can anybody help me fix these issues? Here is my code:

/* This code was written by Johnathon Curnutte. The purpose of this program is to perform a numerical integration of a set of data points from a file to estimate the area under the curve. */ #include #include #include #include #include

using namespace std;

//Function prototypes //double integrate(double a, double b, int n);

int main() { //Declare and initialize objects int num_trapezoids, iterations; double a, a1, b, b1, h, h1, area, base, rectangle, sum, triangle; ifstream fin; string filename; //Get input file from user cout << "Enter name of input file: "; cin >> filename; //cout << "Enter the number of trapezoids you would like to use in this estimation: "; //cin >> num_trapezoids; fin.open(filename.c_str()); if(fin.fail()) { cerr << "Could not open the file, " << filename << "." << " Terminating program... " << endl; exit(1); } while(!fin.eof()) //added { //Obtain initial values fin >> a >> b; cout << b; //Look for more data if(iterations > 0) { base=a-a1; if (b1>b) { h = b; h1 = b1 - b; } if (b >= b1) { h = b1; h1 = b - b1; } //Find total area under the curve triangle = (.5) * base * h1; rectangle = base * h; area = triangle + rectangle; sum = sum + area; } } return (0); }

And here is the input file I use:

x y 0 26 10 28.5 20 28.8 30 29.6 40 32.8 50 40.1 60 46.6 70 42.2 80 48.8 90 52.6 100 55.8 110 60.2 120 60.6 130 58.2 140 53.7 150 50.3 160 45.6 170 45.2 180 43.2 190 38.9 200 35.1 210 30.8 220 27.2 230 21.0 240 22.2 250 18.6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions