The Stimsonian Institute has been flooded with complaints about the number of fatalities caused by their elevator. In order to prove the safety of the elevator, they have asked you to write a simulation of the elevator to mathematically demonstrate its unquestionable safety.
Use the Elevator class from the last lab. If you were absent or didn't do well on the last lab, you can use the model solution instead. Brian Goldman, an Elevator Safety researcher who works with Dr. Chris Roush, conjectured that the safety of an elevator is based on two factors: the ratio of the height by the number of floors (sudden stops make it more likely to crash) and the tendency for the elevator to get stuck. After working out the algebra, Goldman came up with this equation for the relative safety of the elevator:
![]()
where h is the height of each floor and n is the number of floors in the building. Create a new member variable double m_safety as well as a new accessor method double getSafety() const; to access the new member. Create another member function void safetyScore() that will calculate the safety score based on the formula above (if either h or n are 0, set the safety score to 0). Overload the < (less than) operator that will return true if this elevator is less safe than the elevator on the right hand side.
Goldman, like any other engineer, knows that a statistical sampling must be large enough to be significant. Goldman wants you to create an array of 50 random elevators and sort them by safety, with the safest ones at the front of the array. In order to randomize the elevators, overload the ! operator and have it choose random values for the number of floors on the interval [0, 420] and floor height on the interval [0, 4.2). Use the ! operator on each member of the array and then sort it. Then print the best elevator found. Two functions have been provided for you at the end of this lab to help you with this Part.
Read the randDouble() function below closely. Can you mathematically see the approach it's taking? I wrote this function because the mod operator (%) is undefined for the double data type. For Part III, answer the following questions in the comment box when you submit your assignment on Blackboard.
I have provided a templated bubble sort and a function that returns a double on an arbitrary half closed interval. Feel free to use this code to implement Part II.
/* takes in an input array & size and bubble sorts it */
template<class T>
void bubbleSort(T inputArray[], unsigned int size)
{
unsigned int i;
bool swapped = false;
T temp;
do
{
/* assume we haven't swapped anything before bubbling */
swapped = false;
/* bubble through the array */
for (i = 0; i < size - 1; i++)
{
/* bubble the larger element up */
if (inputArray[i] < inputArray[i + 1])
{
swapped = true;
temp = inputArray[i];
inputArray[i] = inputArray[i + 1];
inputArray[i + 1] = temp;
}
}
} while (swapped);
}
/* returns a random number on the interval [low, high) */
double randDouble(double low, double high)
{
double temp;
/* swap low & high around if the user makes no sense */
if (low > high)
{
temp = low;
low = high;
high = temp;
}
/* calculate the random number & return it */
temp = (rand() / (static_cast<double>(RAND_MAX) + 1.0))
* (high - low) + low;
return temp;
}
![[Dilbert]](dilbert.gif)