Question: Define a Rectangle class that provides getLength and getWidth. Using the findMax routines like in the Figure below, write a main that creates an array
Define a Rectangle class that provides getLength and getWidth. Using the findMax routines like in the Figure below, write a main that creates an array of Rectangle and finds the largest Rectangle first on the basis of area and then on the basis of perimeter.
1 // Generic findMax, with a function object, C++ style.
2 // Precondition: a.size( ) > 0.
3 template
4 const Object & findMax( const vector
5 {
6 int maxIndex = 0;
7
8 for(inti=1;i 9 if( isLessThan( arr[ maxIndex ], arr[ i ] ) ) 10 maxIndex = i; 11 12 return arr[ maxIndex ]; 13 } 14 15 // Generic findMax, using default ordering. 16 #include 17 template 18 const Object & findMax( const vector 19 { 20 return findMax( arr, less 21 } 22 23 class CaseInsensitiveCompare 24 { 25 public: 26 bool operator( )( const string & lhs, const string & rhs ) const 27 { return strcasecmp( lhs.c_str( ), rhs.c_str( ) ) < 0; } 28 }; 29 30 int main( ) 31 { 32 vector 33 34 cout << findMax( arr, CaseInsensitiveCompare{ } ) << endl; 35 cout << findMax( arr ) << endl; 36 37 return 0; 38 } --------------------------- output shoud be something like: Largest rectangle with area R[10 20] Largest rectangle with perimeter is R[10 20] ------------------------------------------------------------------------------------ //This is my code so far, feel free to edit or start new. //Rectangle Class class Rectangle { private: int length, width; public: Rectangle() { length=0; width=0; } Rectangle(int l, int w) { length = l; width=w; } int getLength() { return length; } int getWidth() { return width; } int getArea() { return length*width; } int getPerimeter() { return 2*(length+width); } }; Rectangle &findMaxArea(Rectangle rect[], int size, MyComparator c) { int maxIndex=0; for(int i=1; i) if(c.isLessThan(rect[maxIndex].getArea(), rect[i].getArea())) maxIndex=i; return rect[maxIndex]; } Rectangle &findMaxPerimeter(Rectangle rect[], int size, MyComparator c) { int maxIndex=0; for(int i=1; i) if(c.isLessThan(rect[maxIndex].getPerimeter(), rect[i].getPerimeter())) maxIndex=i; return rect[maxIndex]; } int main() { Rectangle r1(2,9), r2(10,20), r3(9,19), r4(5,5), r5(7,6); Rectangle rectangles[5]={r1, r2, r3, r4, r5}; MyComparator comp; Rectangle rect1=findMaxArea(rectangles, 5, comp); cout< Rectangle rect2=findMaxPerimeter(rectangles, 5, comp); cout<< Largest rectangle with perimeter is R[< system(PAUSE); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
