# Define a Rectangle class that provides get Length and get Width. Using the find Max routine with any required modification that you saw in the course, write a main that creates an array of Rectangle and finds the largest Rectangle first based on the area and then based on the perimeter.

33 0

Get full Expert solution in seconds

\$1.97 ONLY

Define a Rectangle class that provides getLength and getWidth. Using the findMax routine with any required modification that you saw in the course, write a main that creates an array of Rectangle and finds the largest Rectangle first based on the area and then based on the perimeter.

**** I have viewed the other similar questions, and I am still getting this error even with using the other questions formatting****

I am getting the following errors:

tempCodeRunnerFile.cpp:100:23: error: non-aggregate type ‘vector<Rectangle>’ cannot be initialized with an initializer list
vector<Rectangle> rectangles = {r1, r2, r3, r4, r5};
^ ~~~~~~~~~~~~~~~~~~~~
tempCodeRunnerFile.cpp:102:57: error: expected ‘(‘ for function-style cast or type construction
Rectangle rect1 = findMax(rectangles, AreaComparator{});
~~~~~~~~~~~~~~^
tempCodeRunnerFile.cpp:106:62: error: expected ‘(‘ for function-style cast or type construction
Rectangle rect2 = findMax(rectangles, PerimeterComparator{});
~~~~~~~~~~~~~~~~~~~^
3 errors generated.

****** Here is my code, please tell me what I am missing/doing wrong:

#include <vector>
#include <iostream>

using namespace std;

template <typename Object, typename Comparator>

const Object & findMax( const vector<Object> & arr, Comparator isLessThan )

{
int maxIndex = 0;

for(int i=1; i < arr.size(); ++i)

if( isLessThan( arr[ maxIndex ], arr[ i ] ) )

maxIndex = i;

return arr[maxIndex];

}

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() const
{
return length * width;

}

int getPerimeter() const {

return 2 * (length + width);

}

};

class AreaComparator

{
public:

bool operator()(const Rectangle & lhs, const Rectangle & rhs ) const {

return lhs.getArea() < rhs.getArea();

}

};

class PerimeterComparator

{

public:

bool operator()(const Rectangle & lhs, const Rectangle & rhs ) const {

return lhs.getPerimeter() < rhs.getPerimeter();

}

};

int main()
{
Rectangle r1(2,9), r2(10,20), r3(9,19), r4(5,5), r5(7,6);

vector<Rectangle> rectangles = {r1, r2, r3, r4, r5};

Rectangle rect1 = findMax(rectangles, AreaComparator{});

cout << “Largest rectangle with area is R[” << rect1.getLength() << ” ” << rect1.getWidth() << “]”;

Rectangle rect2 = findMax(rectangles, PerimeterComparator{});

cout << “\nLargest rectangle with perimeter is R[” << rect2.getLength() << ” ” << rect2.getWidth() << “]”;

return 0;

}

Note: Your code is perfectly running on my computer. I think you are compiling your program using an older c++ compiler like C++98 or C++03. If you will compile same program using newer c++ compilers like C++11 or C++14 or C++17, this program will run properly without giving any errors. But still, if you want to use older compiler only, then small changes are needed in your code.

Here is the code that I think will work on your system.

Only main() function has been slightly modified.

Modified code:

``````#include <vector>
#include <iostream>

using namespace std;

template <typename Object, typename Comparator>

const Object & findMax( const vector<Object> & arr, Comparator isLessThan )

{
int maxIndex = 0;

for(int i=1; i < arr.size(); ++i)

if( isLessThan( arr[ maxIndex ], arr[ i ] ) )

maxIndex = i;

return arr[maxIndex];

}

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() const
{
return length * width;

}

int getPerimeter() const {

return 2 * (length + width);

}

};

class AreaComparator

{
public:

bool operator()(const Rectangle & lhs, const Rectangle & rhs ) const {

return lhs.getArea() < rhs.getArea();

}

};

class PerimeterComparator

{

public:

bool operator()(const Rectangle & lhs, const Rectangle & rhs ) const {

return lhs.getPerimeter() < rhs.getPerimeter();

}

};

int main()
{
Rectangle r1(2,9), r2(10,20), r3(9,19), r4(5,5), r5(7,6);

// vector<Rectangle> rectangles = {r1, r2, r3, r4, r5}; --syntax not supported in old compilers

vector<Rectangle> rectangles(5);
rectangles.push_back(r1);
rectangles.push_back(r2);
rectangles.push_back(r3);
rectangles.push_back(r4);
rectangles.push_back(r5);

//Rectangle rect1 = findMax(rectangles, AreaComparator{}); --syntax not supported in old compilers
Rectangle rect1 = findMax(rectangles, AreaComparator());

cout << "Largest rectangle with area is R[" << rect1.getLength() << " " << rect1.getWidth() << "]";

//Rectangle rect2 = findMax(rectangles, PerimeterComparator{}); --syntax not supported in old compilers
Rectangle rect2 = findMax(rectangles, PerimeterComparator());

cout << "\nLargest rectangle with perimeter is R[" << rect2.getLength() << " " << rect2.getWidth() << "]";

return 0;

}``````

Output:

Largest rectangle with area is R[10 20]
Largest rectangle with perimeter is R[10 20]

Note: As a useful tip, if you compile your program using terminal or command prompt, you can specify version of compiler to be used.

For example, if you want to use c++11 standard, you can write:

g++ tempCodeRunnerFile.cpp -std=c++11