Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the TODO: parts // NumericOverflows.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include //

I need help with the TODO: parts

// 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

//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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions