Objectives: Control structures (if statement, loops), relational & logical opera
ID: 3666456 • Letter: O
Question
Objectives: Control structures (if statement, loops), relational & logical operators, math functions. A point on a plane is represented by two values: x and y. The values determine whether the point is the origin, on x-axis, y-axis, or in one of the four quadrants. Write a C++ program that repeatedly prompts the user to input a point (values for x, and y). The two values represent an x-y coordinate. Your program should output one of the following messages followed by the point. Round the values of x and y to one decimal place.
• The point is the origin (x, y) • The point is on the x-axis (x, y) • The point is on the y-axis (x, y) • The point is in one of the four quadrant (first, second, third, or fourth)
Also, your program should output the point’s distance from the origin. Round the distance to three decimal places and use fixed format. The program should quit when the user chooses to.
Sample input/output: Enter a point (values for x and y) -1 0 Point is on the x-axis (-1.0, 0.0) distance from the origin is 1.000 Continue (y/n)? y Enter a point (values for x and y) 3.1 4 Point is in quadrant I (3.0, 4.0) distance from the origin is 5.061 Continue (y/n)? y Enter a point (values for x and y) -1.5 -2.3 Point is in quadrant III (-1.5, -2.3) distance from the origin is 2.746 Continue (y/n)? n
Explanation / Answer
//header file
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// create class Point
class Point
{
public:
//declare functions as public
Point ( double xP = 0, double yP = 0);
void setPoint(double xP, double yP);
void showPoint();
void setX(double x);
void setY(double y);
double getX();
double getY();
double distanceToOrigin();
double distanceTo(Point p);
void quadrant();
private:
double x;
double y;
};
// constructor
Point::Point(double xP, double yP)
{
x = xP;
y = yP;
}
// setting to the point
void Point :: setPoint(double xP, double yP)
{
x = xP;
y = yP;
}
// display output
void Point :: showPoint()
{
cout << "( " << x << ", " << y << " )" << endl;
}
//setter of x
void Point :: setX(double x)
{
this -> x = x;
}
// setter of y
void Point :: setY(double y)
{
this -> y = y;
}
//getter of x
double Point :: getX()
{
return x;
}
//getter of y
double Point :: getY()
{
return y;
}
// calculate distance to origin
double Point :: distanceToOrigin()
{
double distance = sqrt(((0 - this -> x)*(0 - this -> x)) + ((0 - this -> y)*(0 - this -> y)));
return distance;
}
double Point :: distanceTo(Point p)
{
double distance = sqrt(((p.x - this -> x )*(p.x - this -> x )) + ((p.y - this -> y)*(p.y - this -> y)));
return distance;
}
// define quadrant
void Point :: quadrant()
{
if (this -> x == 0 && this -> y == 0)
{
cout << "(" << this -> x << "," << this -> y << ") is the origin." << " ";
}
/* check for Point on x-axis */
else if(this -> y == 0)
{
cout << "(" << this -> x << "," << this -> y << ") is on the x-axis." << " ";
}
/* check for Point on y-axis */
else if(this -> x == 0)
{
cout << "(" << this -> x << "," << this -> y << ")is on the y axis" <<" ";
}
/* check for quadrant I */
else if(this -> x > 0 && this -> y > 0)
{
cout <<"("<<this -> x <<","<< this -> y <<")is in quadrant I" <<" ";
}
/* check for quadrant II */
else if(this -> x < 0 && this -> y > 0)
{
cout<<"("<<this -> x <<","<< this -> y <<")is in quadrant II" <<" ";
}
/* check for quadrant III */
else if(this -> x < 0 && this -> y < 0)
{
cout<<"("<<this -> x <<","<< this -> y <<")is in quadrant III" <<" ";
}
/* check for quadrant IV */
else
{
cout<<"("<<this -> x <<","<<this -> y <<")is in quadrant IV" <<" ";
}
}
int main()
{
// call function and passing parameters
Point p1(0.0,4.0);
Point p2(-8.0,-4.0);
Point p3(3.5,-6.0);
Point p4(10.0,15.0);
Point p5;
Point origin(0.0,0.0);
p5.setPoint(3.5,-6.0);
Point p6 = p3;
p2.showPoint();
cout << "(" << p2.getX()<< ")" << endl;
cout << "(" << p2.getY()<< ")" << endl;
// display output
cout << "Distance from p3 to origin is " << p3.distanceToOrigin() << endl;
cout << "Distance from p4 to p6 is " << p4.distanceTo(p6) << endl;
// call function
p1.quadrant();
p2.quadrant();
p3.quadrant();
return 0;
}
Sample Output
( -8, -4 )
(-8)
(-4)
Distance from p3 to origin is 6.94622
Distance from p4 to p6 is 21.9829
(0,4)is on the y axis
(-8,-4)is in quadrant III
(3.5,-6)is in quadrant IV
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.