[go: up one dir, main page]

0% found this document useful (0 votes)
36 views2 pages

Structureassignment

The document describes a C++ program that defines a rectangle structure with four point vertices (p1, p2, p3, p4). Each point has an x-coordinate and y-coordinate. The program accepts the coordinates of the four vertices, determines if the rectangle is a square or not, and calculates the area and circumference. It defines Point and Rectangle structures, accepts vertex coordinates as input, checks if it is a square by comparing coordinates, and calculates area as length x width and circumference as 2 x (length + width).

Uploaded by

Feven Fevita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Structureassignment

The document describes a C++ program that defines a rectangle structure with four point vertices (p1, p2, p3, p4). Each point has an x-coordinate and y-coordinate. The program accepts the coordinates of the four vertices, determines if the rectangle is a square or not, and calculates the area and circumference. It defines Point and Rectangle structures, accepts vertex coordinates as input, checks if it is a square by comparing coordinates, and calculates area as length x width and circumference as 2 x (length + width).

Uploaded by

Feven Fevita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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;
}

You might also like