Skip to content

Instantly share code, notes, and snippets.

@geekykant
Last active March 21, 2020 08:02
Show Gist options
  • Save geekykant/01de8aab3740e7be452b70eba2681ba7 to your computer and use it in GitHub Desktop.
Save geekykant/01de8aab3740e7be452b70eba2681ba7 to your computer and use it in GitHub Desktop.
Group 14 (50,52,54,56)
/*
Group 14 (50,52,54,56)
Create a class to hold a point containing member variable to store x and y co-ordinates. Add
necessary member functions to initialize, set and to get values.
Create a class to represent rectangle that contains member variables to hold two instances of point
(top left and bottom right point).Add necessary member function to handle rectangle instance.
Implement a main() to create as many instances of rectangle as per user’s choice dynamically.
Then calculate the perimeter of those rectangles.
Formula is 2(length + breadth).
Then add a global function sort to arrange to display those instances in descending order on the
basis of their perimeter.
*/
#include<iostream>
#include<math.h>
using namespace std;
class Point{
float x,y;
public:
void setValue(){
cin>>x>>y;
}
float getX(){
return x;
}
float getY(){
return y;
}
};
class Rectangle{
public:
Point p1,p2;
Rectangle(Point a, Point b){
p1 = a;
p2 = b;
}
//calculates the origin and the perimeter
float calculatePerimeter(){
return 2 * (abs(p1.getX()-p2.getX()) + abs(p2.getY()-p1.getY()));
}
};
void sort(Rectangle *rect_list[], int n){
for(int i=0; i<n; i++){
for(int j=i+1;j<n;j++){
if(rect_list[i]->calculatePerimeter() < rect_list[j]->calculatePerimeter()){
Rectangle *temp = rect_list[i];
rect_list[i] = rect_list[j];
rect_list[j] = temp;
}
}
}
cout<<endl<<"Descending sorted Rectangles"<<endl;
for (int i = 0; i < n; ++i)
{
cout<<"("<<rect_list[i]->p1.getX()<<","<<rect_list[i]->p1.getY()<<")"
<<","<<"("<<rect_list[i]->p2.getX()<<","<<rect_list[i]->p2.getY()<<")"
<<" Perimeter: "<<rect_list[i]->calculatePerimeter()<<endl;
}
}
int main()
{
int n;
cout<<"Enter number of rectangles: ";
cin>>n;
Rectangle *rect_list[n];
Point p1,p2;
for(int i= 0; i < n; ++i){
cout<<"Enter points for rectangle "<<(i+1)<<" : ";
p1.setValue();
p2.setValue();
rect_list[i] = new Rectangle(p1,p2);
}
sort(rect_list,n);
return 0;
}
@geekykant
Copy link
Author

geekykant commented Mar 21, 2020

INPUT:
3
1 5 5 2
0 1 2 0
0 4 6 0

OUTPUT Screenshot:
Screenshot 2020-03-21 at 1 31 06 PM

PROGRAM:
Enter number of rectangles: 3
Enter points for rectangle 1 : 1 5 5 2
Enter points for rectangle 2 : 0 1 2 0
Enter points for rectangle 3 : 0 4 6 0

Descending sorted Rectangles
(0,4),(6,0) Perimeter: 20
(1,5),(5,2) Perimeter: 14
(0,1),(2,0) Perimeter: 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment