subject

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
// 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)
template
T 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)

template
T 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

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 08:00
The managing director of a company sends a christmas greeting to all his employees through the company email. which type of network does he use? he uses an .
Answers: 3
question
Computers and Technology, 24.06.2019 11:00
Why is it uncommon for users to perform searches directly in database tables? a.)users are discouraged from interacting directly with tables because they might confuse tables with spreadsheets. b.) users are discouraged from interacting directly with tables because this may result in unintended changes to source data. c.)users do not have the technical skills required to perform searches directly in database tables. d.)users do not have the permissions required to perform searches directly in database tables.
Answers: 1
question
Computers and Technology, 24.06.2019 21:30
What need most led to the creation of the advanced research projects agency network? darpa wanted scientists to be able to collaborate and share research easily. darpa wanted american and russian politicians to be able to communicate. darpa wanted astronauts to be able to contact nasa and the white house. darpa wanted factory owners to be able to coordinate supply chains.
Answers: 1
question
Computers and Technology, 25.06.2019 05:50
Acolor class has three public, integer-returning accessor methods: getred, getgreen, and getblue, and three protected, void-returning mutator methods: setred, setgreen, setblue, each of which accepts an integer parameter and assigns it to the corresponding color component. the class, alphachannelcolor-- a subclass of color-- has an integer instance variable, alpha, containing the alpha channel value, representing the degree of transparency of the color. alphachannelcolor also has a method named dissolve (void-returning, and no parameters), that causes the color to fade a bit. it does this by incrementing (by 1) all three color components (using the above accessor and mutator methods) as well as the alpha component value. write the dissolve method.
Answers: 2
You know the right answer?
This assignment presents code that is designed to show the two functions operating with and without...
Questions
question
Mathematics, 24.08.2019 23:50
question
History, 24.08.2019 23:50
Questions on the website: 13722361