Feven Adefris
ECS/1427/23
sub date- Dec ____2023
Sub.to- Mr. Bellilgn Teferi.
#9. Write a program that defines a structure called rectangle with four vertices
(four points p1, p2, p3 and p4).
A point is a data structure consists of two member elements: the X-coordinate and
the Y-Coordinate. The program should accept the four vertices of the rectangle,
determine whether the rectangle is a square or not and also calculate its area and
circumference.
#include <iostream>
#include <cmath>
using namespace std;
// Define the structure for a point
struct Point {
int x;
int y;
};
// Define the structure for a rectangle
struct Rectangle {
struct Point p1, p2, p3, p4;
};
int main() {
struct Rectangle rect;
// Accept the coordinates of four vertices
cout << "Enter coordinates of vertex 1 (x y): ";
cin >> rect.p1.x >> rect.p1.y;
cout << "Enter coordinates of vertex 2 (x y): ";
cin >> rect.p2.x >> rect.p2.y;
cout << "Enter coordinates of vertex 3 (x y): ";
cin >> rect.p3.x >> rect.p3.y;
cout << "Enter coordinates of vertex 4 (x y): ";
cin >> rect.p4.x >> rect.p4.y;
// Determine if the rectangle is a square
if ((rect.p1.x == rect.p2.x && rect.p2.y == rect.p3.y && rect.p3.x == rect.p4.x && rect.p4.y == rect.p1.y) ||
(rect.p1.y == rect.p2.y && rect.p2.x == rect.p3.x && rect.p3.y == rect.p4.y && rect.p4.x == rect.p1.x)) {
cout << "The given rectangle is a square." << endl;
} else {
cout << "The given rectangle is not a square." << endl;
}
// Calculate and display the area and circumference
int side1 = abs(rect.p1.x - rect.p2.x);
int side2 = abs(rect.p2.y - rect.p3.y);
cout << "Area of the rectangle: " << side1 * side2 << " square units" << endl;
cout << "Circumference of the rectangle: " << 2 * (side1 + side2) << " units" << endl;
return 0;
}