Question
Point.h #ifndef _POINT_H_ #define _POINT_H_ #include class Point{ private: double X; double Y; public: Point(); Point(double,double); double Distance(const Point&)const; double getX(); double getY(); }; #endif
Point.h
#ifndef _POINT_H_ #define _POINT_H_ #include
class Point{ private: double X; double Y; public: Point(); Point(double,double); double Distance(const Point&)const; double getX(); double getY(); }; #endif
Point.cpp
#include "Point.h"
Point::Point():X(0),Y(0){}
Point::Point(double _x, double _y):X(_x),Y(_y){}
double Point::Distance(const Point &p)const { return sqrt(pow((X - p.X),2) + pow((Y - p.Y),2)); } double Point::getX() { return X; } double Point::getY() { return Y; }
main.cpp
#include
using namespace std; double** ComputeDistances(const Point*, int); vector
if (!inFS.is_open()) { cout << "Error: can not open the file point." << endl; exit(1); }
vector
while(inFS >> X) { inFS >> Y; Point newP = Point(X,Y); pts.push_back(newP); } return pts; } void DisplayDistances(double** distances,unsigned int size) { for(unsigned int r = 0; r < size; r++){ for(unsigned int c = 0; c < size; c++){ cout << setw(10) << left << distances[r][c]; } if(r != size - 1) cout << endl; } }
Point class contains X and Y member variables that store the coordinates of a point. Given n points as input, you are going to populate a 2D dynamic array that each element of the array determines the Euclidean distance between the corresponding points. GetPointsFromFilefunction reads a file containing a list of 2D points and returns a vector of points. ToDynamicArray function passes in a vector of point and returns a pointer to dynamic array containing the points of the vector. ComputeDistances passes in a pointer to dynamic array and computes a pairwise distances between the points and returns a pointer to 2D array that stores all the distances. Finally, DisplayDistances function outputs the distances to the console.
If the input file contains four points as following (each line stores X and Y values for a point separated by a single space):
0.0 5.0 5.0 0.0 10.0 12.0 3.0 4.5
You are to implement two functions for this lab:
Point* ToDynamicArray(vector
double** ComputeDistances(const Point* pts, int size): Passes in 1D dynamic array of points and returns 2D dynamic array of distances.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started