/* This is a unique program that uses class, and dynamic array
It allows the user to enter however many points they want
At the end, it will ask the user to enter a target point
Then it will calculate the distance between the points
That are different from the target point
*
*/
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
/* Here we declare the class members and constructors
*
*/
class Point
{
public:
void setX(double newx);
void setY(double newy);
double get_X();
double get_Y();
private:
double x, y;
};
/* Here we set the X-Coordinate
*
*/
void Point::setX(double newx)
{
x = newx;
}
/* Here we set the Y-Coordinate
*
*/
void Point::setY(double newy)
{
y = newy;
}
/* Here we gets the X-Coordinate
*
*/
double Point::get_X()
{
return x;
}
/* Here we gets the Y-Coordinate
*
*/
double Point::get_Y()
{
return y;
}
int main()
{
double a,b,c,d;
double dd=0.0;
int n;
Point p;
/* Getting the number of points from the user
*
*/
cout<<"How many points to enter: \n";
cin>>n;
/* Declare two arrays with size n
One array that holds the x-values
Other array that holds the y-values
*
*/
double xvalue[n];
double yvalue[n];
cout<<"\nPlease enter the points one by one with x-coordinator first, then y-coordinator\n";
/* A for loop that gets the coordinates from the user
*
*/
for(int i=0; i<n; i++)
{
cout<<"Point "<<i+1<<": ";
cin>>a;
cin>>b;
p.setX(a);
p.setY(b);
xvalue[i] = p.get_X();
yvalue[i]= p.get_Y();
}
/* Here we ask the user for the target point
*
*/
cout<<"\nPlease enter a target point: ";
cout<<"\nTarget point: ";
cin>>c;
cin>>d;
cout<<"\n";
/* In this for loop we compare the target point and other points
If it is different than the target point, we will calculate
Its distance then we will print out the target point and other points
Include its distance
*
*/
for(int m=0; m<n; m++)
{
if((xvalue[m] == c) && (yvalue[m] ==d))
{
cout<<"Point("<<c<<", "<<d<<") is a target point!\n";
}
else
{
double xd = c - xvalue[m];
double yd = d - yvalue[m];
dd = sqrt(xd*xd + yd*yd);
cout<<"Point " <<"("<<xvalue[m]<<", "<<yvalue[m]<<")"
<<"is not a target point, the distance from ("
<<xvalue[m]<<", "<<yvalue[m]<<")"<<"to"<<"("
<<c<<", "<<d<<"): " <<setprecision(2)<<dd<<"\n";
dd=0.0;
}
}
system("PAUSE");
return 0;
}
Here is the out put of the programHow many points to enter:
3
Please enter the points one by one with x-coordinator first, then y-coordinator
Point 1: 3 5
Point 2: -4 6
Point 3: 0 1
Please enter a target point:
Target point: 0 1
Point (3, 5)is not a target point, the distance from (3, 5)to(0, 1): 5
Point (-4, 6)is not a target point, the distance from (-4, 6)to(0, 1): 6.4
Point(0, 1) is a target point!
Press any key to continue . . .