# Arclength

## Program Listings

• This program will compute (approximately) the length of a curve y = f(x) from a point (a, f(a)) to a point (b, f(b)).
• The program makes use of the point class.

### Arclength.cpp

 ```/************************************************************************ * * * Program: Arclength * * * * Abstract: This program will approximate the arclength of a curve * * * ************************************************************************/ // Header files #include #include #include #include "point.h" // Function prototypes double f(double); /************************************************************************ * * * Function: main * * * ************************************************************************/ int main() { // Get the endpoints of the interval cout << "Enter the endpoints of an interval: " << flush; double a, b; cin >> a >> b; // Get the number of subintervals used in the approximation cout << "Enter the number of subintervals: " << flush; int n; cin >> n; // Determine the width of a subinterval double deltaX = (b - a)/n; double length = 0; point prevPt(a, f(a)); double x = a; // For each subinterval, find the straight-line distance between // two points on the curve. Add the distance to the total. for (int i = 1; i <= n; i++) { x += deltaX; // Increment x point nextPt(x, f(x)); // Get right endpoint length += prevPt.Distance(nextPt); // Add distance to total prevPt = nextPt; // Get next left endpt } // Report the arclength cout << endl << "The arclength from " << point(a, f(a)) << " to "; cout << point(b, f(b)) << " is approximately " << length << endl; return 0; } /************************************************************************ * * * Function: f * * * * Purpose: To define the curve y = f(x) * * * ************************************************************************/ double f(double x) { return 2*pow(x, 2) + 1; // The function y = 2x^2 + 1 }```

### point.h

 ```/************************************************************************ * * * File: point.h * * * * Author: Robb T. Koether * * * * Date: Oct 19, 1999 * * * * Abstract: This file contains the definition of the point class * * and related operators * * * ************************************************************************/ #ifndef POINT_H #define POINT_H // Header files #include #include #include using namespace std; /************************************************************************ * * * Definition of the point class * * * ************************************************************************/ class point { public: // Constructors point(); // Default constructor point(double x, double y); // Construct point from 2 floats // Inspectors double GetX() const; // Return the x-coordinate double GetY() const; // Return the y-coordinate // Mutators void SetX(double x); // Set the x-coordinate void SetY(double y); // Set the y-coordinate // Facilitators void Input(istream& in); // Extract this pt from istream void Output(ostream& out) const; // Insert this pt into ostream bool Equal(const point& p) const; // See if pt equals this pt point Add(const point& p) const; // Add pt to this pt point Multiply(double s) const; // Multiply this pt by scalar // Other member functions double Distance(const point& p) const; // Distance between points private: double xval; // x-coordinate of the point double yval; // y-coordinate of the point }; /************************************************************************ * * * Operators * * * ************************************************************************/ // Input-Output Operators istream& operator>>(istream& in, point& p); ostream& operator<<(ostream& out, const point& p); // Equality Operators bool operator==(const point& p, const point& q); bool operator!=(const point& p, const point& q); // Arithmetic Operators point operator+(const point& p, const point& q); point operator*(double s, const point& p); #endif```

### Point.cpp

 ```/************************************************************************ * * * File: Point.cpp * * * * Author: Robb T. Koether * * * * Date: Oct 19, 1999 * * * * Abstract: This file implements the member functions and * * operators of the point class * * * ************************************************************************/ // Header files #include "point.h" #include "assert.h" /************************************************************************ * * * CONSTRUCTORS * * * ************************************************************************/ /************************************************************************ * * * Function: point() * * * * Purpose: To construct the default point (0, 0) * * * ************************************************************************/ point::point() { xval = 0; yval = 0; return; } /************************************************************************ * * * Function: point(double, double) * * * * Purpose: To construct a point with the specified x- and * * y-coordinates * * * ************************************************************************/ point::point(double x, double y) { xval = x; yval = y; return; } /************************************************************************ * * * INSPECTORS * * * ************************************************************************/ /************************************************************************ * * * Function: GetX * * * * Purpose: To return the x-coordinate of the point * * * ************************************************************************/ double point::GetX() const { return xval; } /************************************************************************ * * * Function: GetY * * * * Purpose: To return the y-coordinate of the point * * * ************************************************************************/ double point::GetY() const { return yval; } /************************************************************************ * * * MUTATORS * * * ************************************************************************/ /************************************************************************ * * * Function: SetX * * * * Purpose: To set the x-coordinate of the point * * * ************************************************************************/ void point::SetX(double x) { xval = x; return; } /************************************************************************ * * * Function: SetY * * * * Purpose: To set the y-coordinate of the point * * * ************************************************************************/ void point::SetY(double y) { yval = y; return; } /************************************************************************ * * * FACILITATORS * * * ************************************************************************/ /************************************************************************ * * * Function: Input * * * * Purpose: To extract a point from the specified input stream * * * ************************************************************************/ void point::Input(istream& in) { char c; in >> c; // Read open parenthesis assert(c == '('); // Verify open parenthesis in >> xval; // Read x-coordinate in >> c; // Read comma assert(c == ','); // Verify comma in >> yval; // Read y-coordinate in >> c; // Read close parenthesis assert(c == ')'); // Verify close parenthesis return; } /************************************************************************ * * * Function: Output * * * * Purpose: To insert a point into the specified output stream * * * ************************************************************************/ void point::Output(ostream& out) const { out << '(' << xval << ", " << yval << ')'; return; } /************************************************************************ * * * Function: Equal * * * * Purpose: To determine whether two points are equal * * * ************************************************************************/ bool point::Equal(const point& p) const { return (xval == p.xval) && (yval == p.yval); } /************************************************************************ * * * Function: Add * * * * Purpose: To add two points together coordinate-wise * * * ************************************************************************/ point point::Add(const point& p) const { double x = xval + p.xval; double y = yval + p.yval; return point(x, y); } /************************************************************************ * * * Function: Multiply * * * * Purpose: To multiply a point by a scalar * * * ************************************************************************/ point point::Multiply(double s) const { double x = s * xval; double y = s * yval; return point(x, y); } /************************************************************************ * * * OTHER MEMBER FUNCTIONS * * * ************************************************************************/ /************************************************************************ * * * Function: Distance * * * * Purpose: To find the distance between two points * * * ************************************************************************/ double point::Distance(const point& p) const { return sqrt(pow(xval - p.xval, 2) + pow(yval - p.yval, 2)); } /************************************************************************ * * * INPUT-OUTPUT OPERATORS * * * ************************************************************************/ /************************************************************************ * * * Function: operator>>(istream, point) * * * * Purpose: To extract a point from the specified input stream * * * ************************************************************************/ istream& operator>>(istream& in, point& p) { p.Input(in); return in; } /************************************************************************ * * * Function: operator<<(ostream, point) * * * * Purpose: To insert a point into the specified output stream * * * ************************************************************************/ ostream& operator<<(ostream& out, const point& p) { p.Output(out); return out; } /************************************************************************ * * * EQUALITY OPERATORS * * * ************************************************************************/ /************************************************************************ * * * Function: operator==(point, point) * * * * Purpose: To determine whether two points are equal * * * ************************************************************************/ bool operator==(const point& p, const point& q) { return p.Equal(q); } /************************************************************************ * * * Function: operator!=(point, point) * * * * Purpose: To determine whether two points are unequal * * * ************************************************************************/ bool operator!=(const point& p, const point& q) { return !p.Equal(q); } /************************************************************************ * * * ARITHMETIC OPERATORS * * * ************************************************************************/ /************************************************************************ * * * Function: operator+(point, point) * * * * Purpose: To add two points together coordinate-wise * * * ************************************************************************/ point operator+(const point& p, const point& q) { return p.Add(q); } /************************************************************************ * * * Function: operator*(double, point) * * * * Purpose: To multiply a point by a scalar * * * ************************************************************************/ point operator*(double s, const point& p) { return p.Multiply(s); }```

### Sample Run

 ```Enter the endpoints of an interval: 0 2 Enter the number of subintervals: 100 The arclength from (0, 1) to (2, 9) is approximately 8.40925 ```