\"Write a C++ program that determines whether or not a given point is contained
ID: 668561 • Letter: #
Question
"Write a C++ program that determines whether or not a given point is contained in a given rectangle."
---------------------------------------------------------------------------------------------------------------------------------------------------
*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
----------------------------------------------------------------------------------------------------------------------------------------------------
Here is my logic so far:
if(x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0 || x2 < y2 || y2 < y1 || x3 < 0 || y3 < 0)
{error;}
if(x3 > x1 && x3 <= x2 && y3 >= y1 && y3 <= y2) {
}
else {
}
if (x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0) {
break;
}
Explanation / Answer
#include<iostream>
using namespace std;
/*
()
*(x1,y1)-------------------------
|
|
|
|-------------------------(x2,y2)*
*/
int main()
{
int x1,x2,x,y1,y2,y;
while(true)
{
cout << "Enter upper left corner x cordinate : " ;
cin >> x1;
cout << "Enter upper left corner y cordinate : " ;
cin >> y1;
cout << "Enter lower right corner x cordinate : " ;
cin >> x2;
cout << "Enter lower right corner y cordinate : " ;
cin >> y2;
cout << "Enter x cordinate to be checked : " ;
cin >> x;
cout << "Enter y cordinate to be checked : " ;
cin >> y;
if(x1 == 0 && y1 == 0 && x2 == 0 && y2 ==0)
{
cout << "Exitting the program ";
break;
}
if(x>=x1&&x<=x2&&y>=y2&&y<=y1)
cout << "Point is inside the rectangle ";
else
cout << "Point is not inside the rectangel ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.