/* A version of array_path.cpp that uses a struct Point to store the each point, instead of separately storing the x and y coordinates. Note the syntax for using an array of structs. Also, the function path_length is simpler in this version, since it doesn't have to refer to the x and y coordinates explicitly. */ #include #include #include const int NUM_POINTS = 10; struct Point{ public: double x_coord, y_coord; }; double line_seg_length (Point, Point); double path_length(Point[]); void main(){ ifstream infile("points.dat"); Point pts[NUM_POINTS]; for (int i = 0; i< NUM_POINTS; i++) infile >> pts[i].x_coord >> pts[i].y_coord; cout << "The length of the connect-the-dots path starting at\npoint 0" << " and ending at point " << NUM_POINTS - 1 << " is " << path_length(pts) << ".\n"; } double line_seg_length (Point pt1, Point pt2){ return sqrt(pow(pt2.x_coord - pt1.x_coord,2) + pow(pt2.y_coord - pt1.y_coord,2)); } double path_length (Point pts[]){ double length = 0; for (int i=0; i < NUM_POINTS - 1; i++) length += line_seg_length(pts[i], pts[i+1]); return length; }