C++ programming A rectangle is completely determined by the coordinates of two o
ID: 3884448 • Letter: C
Question
C++ programming
A rectangle is completely determined by the coordinates of two of its diagonally opposite corners. For example, the points (1, 2) and (7, 5) determine a rectangle whose left edge has equation x = 1, whose right edge has equation x = 7, whose bottom edge has equation y = 2 and whose top edge has equation y = 5. Any point whose x-coordinate is between 1 and 7 and whose y-coordinate is between 2 and 5 lies within this rectangle.
Write a program that determines whether or not a given point is contained in a given rectangle.
Your program should:
Prompt the user for the upper left corner of a rectangle.
Prompt the user for the lower right corner of the rectangle.
Prompt the user for the coordinates of a point.
Output whether or not the point is inside the rectangle.
Repeat the above steps until the user enters (0, 0) for both corners of the rectangle.
Assume that all coordinates are non-negative integers with (0, 0) being the extreme upper-right corner.
Be readable with appropriate documentation and formatting.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int xul,yul,xlr,ylr,x,y;
cout<<"Enter the x and y cooordinates of upper left corner of a rectangle : ";
cin>>xul>>yul;
cout<<" Enter the x and y coordinates of lower right corner of the rectangle : ";
cin>>xlr>>ylr;
cout<<" Enter the coordinates of a point : ";
cin>>x>>y;
if(x>=xul && x<=xlr && y>=ylr && y<=yul) // xul<x<xlr , ylr <y<yul
cout<<" Point with coordinates "<<x<<","<<y<<" are in Rectangle";
else
cout<<" Point with coordinates "<<x<<","<<y<<" are not in Rectangle";
return 0;
}
output:
Enter the x and y cooordinates of upper left corner of a rectangle : 1 5
Enter the x and y coordinates of lower right corner of the rectangle : 7 2
Enter the coordinates of a point : 3 4
Point with coordinates 3,4 are in Rectangle
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.