Question: FUNCTION DESCRIPTION Given two numbers a and b, calculate the number that is two-thirds the way toward the larger number If the numbers are 2
FUNCTION DESCRIPTION Given two numbers a and b, calculate the number that is two-thirds the way toward the larger number
If the numbers are 2 and 11, then the result would be 8
If the numbers are 1.0 and 0.1 then the result would be 0.7
If the numbers are 0.1 and 1.0 then the result would be 0.7
If the numbers are 4 and 4, then the result would be 4
PART ONE Develop a "normal" function named TwoThirdsBetweenTwoDoubles that does the following:
INPUT PARAMETER - DOUBLE a
INPUT PARAMETER - DOUBLE b
RETURN VALUE - DOUBLE that represents the result value two-thirds the way toward the larger input parameter
EXCEPTION - Throw an std::out_of_range exception if the result value is negative
TwoThirdsBetweenTwoDoubles(5.0, 5.0) == 5.0
TwoThirdsBetweenTwoDoubles(0.0, 12.0) == 8.0
TwoThirdsBetweenTwoDoubles(12.0, 0.0) == 8.0
TwoThirdsBetweenTwoDoubles(-9.0, 3.0) will throw an exception since the result -1.0 is negative
There are many test cases in the starting file that should pass when your code is written that accommodates these instructions Keep this code in your submission, and you don't even have to comment it out or anything Copy this code and use the copy as your starting point for the second part below
PART TWO Develop a "templated" function named TwoThirdsBetweenTwoTs that does the following:
Change the DOUBLE types in part one into ones that are a TEMPLATED T TYPE
INPUT PARAMETER - T a
INPUT PARAMETER - T b
RETURN VALUE - T that represents the result value two-thirds the way toward the larger input parameter
EXCEPTION - Throw an std::out_of_range exception if the result value is negative
Depending on how you set up your function, not every variable inside it needs to be changed
Remember that integer division truncates the fractional part of the result value
TwoThirdsBetweenTwoTs(0, 10) == 6
TwoThirdsBetweenTwoTs(18.3, 0.0) == 12.2
TwoThirdsBetweenTwoTs(-18.0, 3.0) will throw an exception since the result -4.0 is negative
Now your code should be able to work for any elemental data type like int, double, and float There is a second round of test cases that need to work properly for full points All successful test cases means you're very likely to have full points
Use this runner file as the starting point for your solution to this problem :
#include #include // --------------------------------------------------- // IMPLEMENT YOUR PART ONE NON-TEMPLATED FUNCTION HERE // --------------------------------------------------- // ----------------------------------------------- // IMPLEMENT YOUR PART TWO TEMPLATED FUNCTION HERE // ----------------------------------------------- // -------------------------------------------------------------------------------- // EVERYTHING BELOW HERE ARE TEST CASES THAT ENSURE YOUR FUNCTIONS WORK AS EXPECTED // YOU MAY NOT MODIFY ANY OF THE CODE FOUND BELOW THIS POINT // -------------------------------------------------------------------------------- // Floating point imprecision can be a pain // If the two numbers are closer to each other than float imprecision we agree they're equal bool IsCloseEnough(double a, double b, double EPSILON = 0.000001) { return abs(a - b) <= EPSILON; } void PartOneTesting() { // Result can be the same as both inputs assert(TwoThirdsBetweenTwoDoubles(5.0, 5.0) == 5.0); // Result two thirds of the way toward the larger value assert(TwoThirdsBetweenTwoDoubles(0.0, 12.0) == 8.0); assert(TwoThirdsBetweenTwoDoubles(12.0, 0.0) == 8.0); // Floating point imprecision can be a pain assert(IsCloseEnough(TwoThirdsBetweenTwoDoubles(1.0, 2.0), 1.6666666) == true); // Result can be 0.0 assert(TwoThirdsBetweenTwoDoubles(-8.0, 4.0) == 0.0); try { // Result can't be negative though double d = TwoThirdsBetweenTwoDoubles(-9.0, 3.0); // You're not supposed to get here... assert(false && "Your function needs to throw an std::out_of_range exception for negative results"); } catch (const std::bad_alloc&) { // You're not supposed to get here... assert(false && "You are not supposed to catch this exception"); } catch (const std::out_of_range&) { // You're supposed to catch the correct exception your function throws std::cout << "ALL PART ONE TESTS PASSED!" << std::endl; } } void PartTwoTesting() { // Result can be the same as both inputs assert(TwoThirdsBetweenTwoTs(2.0, 2.0) == 2.0); assert(TwoThirdsBetweenTwoTs(7, 7) == 7); // Result two thirds the way toward the larger value assert(TwoThirdsBetweenTwoTs(0.0, 18.0) == 12.0); assert(TwoThirdsBetweenTwoTs(12.0, 3.0) == 9.0); assert(TwoThirdsBetweenTwoTs(0, 9) == 6); assert(TwoThirdsBetweenTwoTs(21, 12) == 18); // Floating point imprecision can be a pain assert(IsCloseEnough(TwoThirdsBetweenTwoTs(2.0, 3.0), 2.6666666) == true); // Remember integer division is a truncation operation assert(IsCloseEnough(TwoThirdsBetweenTwoTs(2, 3), 2) == true); assert(IsCloseEnough(TwoThirdsBetweenTwoTs(0, 100), 66) == true); // Result can be 0.0 assert(TwoThirdsBetweenTwoTs(-12.0, 6.0) == 0.0); assert(TwoThirdsBetweenTwoTs(-100, 50) == 0.0); try { // Result can't be negative though double d = TwoThirdsBetweenTwoTs(-19.0, 3.0); // You're not supposed to get here... assert(false && "Your function needs to throw an std::out_of_range exception for negative results"); } catch (const std::out_of_range&) { // Which means you're supposed to catch the exception your function throws // Do nothing with it because we need to move to the next test case } try { // Result can't be negative double d = TwoThirdsBetweenTwoTs(3, -92); // You're not supposed to get here... assert(false && "Your function needs to throw an std::out_of_range exception for negative results"); } catch (const std::out_of_range&) { // Which means you're supposed to catch the exception your function throws std::cout << "ALL PART TWO TESTS PASSED!" << std::endl; } } int main() { PartOneTesting(); PartTwoTesting(); std::cout << "ALL TESTS PASSED!" << std::endl; std::cout << "LOOKS LIKE YOU'RE ALL SET AND READY TO SUBMIT!" << std::endl; return 0;