This lab is due by Wednesday, April 13, 2022 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.
Instructions
For this lab, we will continue to make our sorting more flexible.
Begin by creating a Rectangle class. The class will need data members to store the base and height (as floats) plus the appropriate getters/setters for each. Have the constructor set each to be 1 by default.
We will want to be able to compare two Rectangles as well. Overload the < and > operators. These operators are resolved as follows:
-
rect1 < rect2
:rect1
is less thanrect2
if thebase
ofrect1
is less than thebase
ofrect2
OR thebase
s are equal and theheight
ofrect
is less than theheight
ofrect2
-
rect1 > rect2
:rect1
is greater thanrect2
if thebase
ofrect1
is greater than thebase
ofrect2
OR thebase
s are equal and theheight
ofrect
is greater than theheight
ofrect2
Make a list of Rectangles (array or LinkedList). Now modify one of your previous sort methods (selection, insertion, bubble, merge) to accept a function pointer for how to do the comparison when sorting. Create an ascending function and a descending function that can be used as the comparing function.
To test your implementations, perform the following steps:
- Ask the user how many Rectangles to create
- Create a list of
n
Rectangles - Assign each a random base and height
- Print the list forwards (print the rectangle as
base
xheight
) - Sort the list ascending
- Print the list forwards (print the rectangle as
base
xheight
) - Sort the list descending
- Print the list forwards (print the rectangle as
base
xheight
)
Lab Submission
Submit your main.cpp, Makefile, *.hpp, *.h, *.cpp
file(s).
You will submit your solution to this lab with the rest of Set5. Detailed instructions for doing this are posted in Assignment 5.
This lab is due by Wednesday, April 13, 2022 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.