Question: Overview You are a senior software developer on a team of software developers who are responsible for a large banking application. Your manager has learned
Overview
You are a senior software developer on a team of software developers who are responsible for a large banking application. Your manager has learned that the code used for addition and subtraction over a series of steps will sometimes calculate incorrect results. The other developers have spent a lot of time tracing down the issue, and they narrowed it down to two functions: add_numbers() and subtract_numbers(). Following some guidance from an older testing guide, they isolated those functions in a standalone set of test cases. Doing this allows them to reliably show how to trigger an underflow or overflow. However, they dont know what to do to prevent it from occurring in the future. They came to you for help.
You will learn to do the following:
Detect when a numeric overflow or underflow is about to happen
Write code to prevent the numeric overflow or underflow from happening
Protect your code from underflow and overflow in any of the standard C/C++ data types
Prompt
This assignment presents code that is designed to show the two functions operating with and without overflow or underflows. Using the existing source code, you will add logic to the add_numbers() and subtract_numbers() functions to detect, prevent, and notify the caller of a numeric overflow. You will also modify the calling test_overflow and test_underflow functions to be notified of success or failure of the add or subtract functions they call, and to modify them, the functions write to the console with overflow status (true or false) and the numeric result of the function.
The following are a few key notes:
The source code has been commented with TODOs to explain the detailed rules you must follow.
There are comments that mark code that must be changed.
There may be more than one way to solve this problem, so be sure to demonstrate that you can detect an underflow or overflow, prevent it, and communicate it back to the calling function.
Remember to leverage capabilities provided by the standard C++ library to help you achieve success.
Please comment on any changes you make in the code to explain the logic, formulas, or data types you are adding. You will also create a brief written summary of the approach taken. It should explain how this approach is designed to stop the overflow or underflow, any issues you encountered, and how you resolved those issues.
Specifically, you will prepare the following:
Numeric overflow and underflow secure coding
C/C++ program functionality and best practices
A summary of your process in a Word document that contains a screenshot of the application console output
To complete this assignment, download the Numeric Overflow source code to use as guidance as you move through the activity. You will use your development environment to complete this activity.
GIVEN CODE BELOW:
// NumericOverflows.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include// std::cout #include // std::numeric_limits /// /// Template function to abstract away the logic of: /// start + (increment * steps) /// ///A type that with basic math functions /// The number to start with /// How much to add each step /// The number of steps to iterate ///start + (increment * steps) templateT add_numbers(T const& start, T const& increment, unsigned long int const& steps) { T result = start; for (unsigned long int i = 0; i < steps; ++i) { result += increment; } return result; } /// /// Template function to abstract away the logic of: /// start - (increment * steps) /// ///A type that with basic math functions /// The number to start with /// How much to subtract each step /// The number of steps to iterate ///start - (increment * steps) templateT subtract_numbers(T const& start, T const& decrement, unsigned long int const& steps) { T result = start; for (unsigned long int i = 0; i < steps; ++i) { result -= decrement; } return result; } // NOTE: // You will see the unary ('+') operator used in front of the variables in the test_XXX methods. // This forces the output to be a number for cases where cout would assume it is a character. template void test_overflow() { // TODO: The add_numbers template function will overflow in the second method call // You need to change the add_numbers method to: // 1. Detect when an overflow will happen // 2. Prevent it from happening // 3. Return the correct value when no overflow happened or // 4. Return something to tell test_overflow the addition failed // NOTE: The add_numbers method must remain a template in the NumericFunctions header. // // You need to change the test_overflow method to: // 1. Detect when an add_numbers failed // 2. Inform the user the overflow happened // 3. A successful result displays the same result as before you changed the method // NOTE: You cannot change anything between START / END DO NOT CHANGE // The test_overflow method must remain a template in the NumericOverflows source file // // There are more than one possible solution to this problem. // The solution must work for all of the data types used to call test_overflow() in main(). // START DO NOT CHANGE // how many times will we iterate const unsigned long int steps = 5; // how much will we add each step (result should be: start + (increment * steps)) const T increment = std::numeric_limits ::max() / steps; // whats our starting point const T start = 0; std::cout << "Overflow Test of Type = " << typeid(T).name() << std::endl; // END DO NOT CHANGE std::cout << "\tAdding Numbers Without Overflow (" << +start << ", " << +increment << ", " << steps << ") = "; T result = add_numbers (start, increment, steps); std::cout << +result << std::endl; std::cout << "\tAdding Numbers With Overflow (" << +start << ", " << +increment << ", " << (steps + 1) << ") = "; result = add_numbers (start, increment, steps + 1); std::cout << +result << std::endl; } template void test_underflow() { // TODO: The subtract_numbers template function will underflow in the second method call // You need to change the subtract_numbers method to: // 1. Detect when an underflow will happen // 2. Prevent it from happening // 3. Return the correct value when no underflow happened or // 4. Return something to tell test_underflow the subtraction failed // NOTE: The subtract_numbers method must remain a template in the NumericFunctions header. // // You need to change the test_underflow method to: // 1. Detect when an subtract_numbers failed // 2. Inform the user the underflow happened // 3. A successful result displays the same result as before you changed the method // NOTE: You cannot change anything between START / END DO NOT CHANGE // The test_underflow method must remain a template in the NumericOverflows source file // // There are more than one possible solution to this problem. // The solution must work for all of the data types used to call test_overflow() in main(). // START DO NOT CHANGE // how many times will we iterate const unsigned long int steps = 5; // how much will we subtract each step (result should be: start - (increment * steps)) const T decrement = std::numeric_limits ::max() / steps; // whats our starting point const T start = std::numeric_limits ::max(); std::cout << "Underflow Test of Type = " << typeid(T).name() << std::endl; // END DO NOT CHANGE std::cout << "\tSubtracting Numbers Without Overflow (" << +start << ", " << +decrement << ", " << steps << ") = "; auto result = subtract_numbers (start, decrement, steps); std::cout << +result << std::endl; std::cout << "\tSubtracting Numbers With Overflow (" << +start << ", " << +decrement << ", " << (steps + 1) << ") = "; result = subtract_numbers (start, decrement, steps + 1); std::cout << +result << std::endl; } void do_overflow_tests(const std::string& star_line) { std::cout << std::endl << star_line << std::endl; std::cout << "*** Running Overflow Tests ***" << std::endl; std::cout << star_line << std::endl; // Testing C++ primative times see: https://www.geeksforgeeks.org/c-data-types/ // signed integers test_overflow (); test_overflow (); test_overflow (); test_overflow (); test_overflow (); test_overflow (); // unsigned integers test_overflow (); test_overflow (); test_overflow (); test_overflow (); test_overflow (); // real numbers test_overflow (); test_overflow (); test_overflow (); } void do_underflow_tests(const std::string& star_line) { std::cout << std::endl << star_line << std::endl; std::cout << "*** Running Undeflow Tests ***" << std::endl; std::cout << star_line << std::endl; // Testing C++ primative times see: https://www.geeksforgeeks.org/c-data-types/ // signed integers test_underflow (); test_underflow (); test_underflow (); test_underflow (); test_underflow (); test_underflow (); // unsigned integers test_underflow (); test_underflow (); test_underflow (); test_underflow (); test_underflow (); // real numbers test_underflow (); test_underflow (); test_underflow (); } /// /// Entry point into the application /// ///0 when complete int main() { // create a string of "*" to use in the console const std::string star_line = std::string(50, '*'); std::cout << "Starting Numeric Underflow / Overflow Tests!" << std::endl; // run the overflow tests do_overflow_tests(star_line); // run the underflow tests do_underflow_tests(star_line); std::cout << std::endl << "All Numeric Underflow / Overflow Tests Complete!" << std::endl; return 0; } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu
PLEASE FIX TODO LIST PART OF THE CODE.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
