Question: #include #include #include #include #include using namespace std; void getPoint(int& x, int& y); void displayPoint(int x, int y); double getDistance (int x1, int y1, int
#include
#include
#include
#include
#include
using namespace std;
void getPoint(int& x, int& y);
void displayPoint(int x, int y);
double getDistance (int x1, int y1, int x2, int y2);
void orderLeftToRight(int& x1, int& y1, int& x2, int& y2);
void findClosestToOrigin(int x1, int y1, int x2, int y2,
int& closestX, int& closestY);
/****************************************************************************
Function: main
Description: Performs some calculations on points
Parameters: None
Returned: Exit Status
****************************************************************************/
int main() {
int x1, y1;
int x2, y2;
int closestX, closestY;
cout << setprecision(3) << fixed;
cout << "********************* ";
cout << "* POINTS * ";
cout << "********************* ";
cout << endl;
getPoint(x1, y1);
getPoint(x2, y2);
cout << " You entered points ";
displayPoint(x1, y1);
cout << " and ";
displayPoint(x2, y2);
cout << endl;
getDistance(x1, y1, x2, y2);
cout << endl;
orderLeftToRight(x1, y1, x2, y2);
cout << endl;
findClosestToOrigin(x1, y1, x2, y2, closestX, closestY);
cout << "(" << closestX << ", " << closestY << ")" << endl;
system("Pause");
return EXIT_SUCCESS;
}
/****************************************************************************
Function: getPoint
Description: Prompt the user for a point (x,y). Read the point from the user
Parameters: x - the x value of the point, pass by reference
y - the y value of the point, pass by reference
Returned: None
****************************************************************************/
void getPoint(int& x, int& y) {
cout << "Enter a point: ";
cin >> x >> y;
}
/****************************************************************************
Function: displayPoint
Description: Display the point to the screen
Parameters: x - the x value of the point
y - the y value of the point
Returned: None
****************************************************************************/
void displayPoint(int x, int y) {
cout << "(" << x << ", " << y << ")";
}
/****************************************************************************
Function: getDistance
Description: Calculate the distance between the points. Return distance.
Parameters: x1 - the x value of the first point
y1 - the y value of the first point
x2 - the x value of the second point
y2 - the y value of the second point
Returned: the distance between the points
****************************************************************************/
double getDistance(int x1, int y1, int x2, int y2) {
double x, y;
double s1,s2 , result;
cout << " ";
cout << endl;
cout << "The distance between the points is";
x = x2 - x1;
y = y2 - y1;
s1 = x2- x1;
s2 = y2 - y1;
result = sqrt(s1) + sqrt(s2);
printf ("(%f)", result);
cout << endl;
return 0.0;
}
/****************************************************************************
Function: orderLeftToRight
Description: Order the points from left to right using the x values.
The left most point ends up in x1, y1, the rightmost point
ends up in x2, y2
Parameters: x1 - the x value of the first point
y1 - the y value of the first point
x2 - the x value of the second point
y2 - the y value of the second point
Returned: None
****************************************************************************/
void orderLeftToRight(int& x1, int& y1, int& x2, int& y2) {
cout << "The points left to right are";
cout << "(" << x1 << ", " << y1 << ")" << " and " << "(" << x2 << ", " << y2 << ")";
cout << endl;
}
/****************************************************************************
Function: findClosestToOrigin
Description: Determine which of two points is closer to (0, 0).
Return the point via closestX, closestY
Parameters: x1 - the x value of the first point
y1 - the y value of the first point
x2 - the x value of the second point
y2 - the y value of the second point
closestX - the x value of the point closest to the origin
closestY - the y value of the point closest to the origin
Returned: None
****************************************************************************/
void findClosestToOrigin(int x1, int y1, int x2, int y2,
int& closestX, int& closestY) {
cout << "The point closest to the origin is:";
if (x1 <= x2) {
closestX = x1;
}
if (y1 <= y2) {
closestY = y1;
}
if (x2 <= x1) {
closestX = x2;
}
if (y2 <= y1) {
closestY = y2;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
