Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming A rectangle is completely determined by the coordinates of two o

ID: 3882807 • 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

// program to check the coordinates in rectangle or not

#include <cstdlib>
#include <iostream>

using namespace std;
/*
* to check whether or not the coordinates of a point
* lies is inside the rectangle.
*/

int main(int argc, char** argv) {
// declare the variable upper, lower, value for the rectangle co ordinates

struct rect {
int x;
int y;
} upper, lower, value;
// to perform the function till all the values are 0
do {
cout << " (To exit enter(0,0)for both co ordinates )";

//the co-ordinate for upper left corner
cout << " Enter the co-ordinates of upper left corner of the rectangle ";
cout << " x :";
cin >> upper.x;
cout << " y :";
cin >> upper.y;
//the co-ordinate for lower right corner
cout << " Enter the co-ordinates for lower right corner of the rectangle";
cout << " x :";
cin >> lower.x;
cout << " y :";
cin >> lower.y;
/*check whether co ordinates are not 0.
*if not 0 then accept the co-ordinate value &
* check whether or point lies inside the rectangle
*/
if ((upper.x != 0)&&(upper.y != 0)&&(lower.y != 0)&&(lower.x != 0)) {
// accept the co-ordinate point to be checked
cout << " Enter the co-ordinates of the point";
cout << " x :";
cin >> value.x;
cout << " y :";
cin >> value.y;
// display the upper and lower co ordinate
cout << " The upper co-ordinate (" << upper.x << ", " << upper.y << " )& lower coordinate (" << lower.x << ", " << lower.y << " )";
if (((value.x >= upper.x)&&(value.x <= lower.x)) &&((value.y >= upper.y)&&(value.y <= lower.y)))
cout << " The given co-ordinate (" << value.x << " , " << value.y << ") lies in the rectangle";
else
cout << " The given co-ordinate (" << value.x << " , " << value.y << ") doesn't lies in the rectangle";
}
} while ((upper.x != 0)&&(upper.y != 0)&&(lower.y != 0)&&(lower.x != 0));
return 0;
}

sample output

(To exit enter(0,0)for both co ordinates )
Enter the co-ordinates of upper left corner of the rectangle
x :1

y :2

Enter the co-ordinates for lower right corner of the rectangle
x :7

y :5

Enter the co-ordinates of the point
x :5

y :3

The upper co-ordinate (1, 2 )& lower coordinate (7, 5 )
The given co-ordinate (5 , 3) lies in the rectangle
(To exit enter(0,0)for both co ordinates )
Enter the co-ordinates of upper left corner of the rectangle
x :1

y :2

Enter the co-ordinates for lower right corner of the rectangle
x :7

y :5

Enter the co-ordinates of the point
x :8

y :4

The upper co-ordinate (1, 2 )& lower coordinate (7, 5 )
The given co-ordinate (8 , 4) doesn't lies in the rectangle
(To exit enter(0,0)for both co ordinates )
Enter the co-ordinates of upper left corner of the rectangle
x :0

y :0

Enter the co-ordinates for lower right corner of the rectangle
x :0

y :0

RUN SUCCESSFUL (total time: 1m 2s)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote