Question

In: Computer Science

(a) Provide a detailed explanation of the (signed) integer overflow. (b) Write C++ program that displays...

(a) Provide a detailed explanation of the (signed) integer overflow. (b) Write C++ program that displays the table of function 2n . This is an integer value that you must compute using multiplication, and the use of power function pow(2.,n ) is not permitted. Display lines that contain n and 2n for n=0,1,2,3,… and show all lines that contain correct values. Stop displaying before the occurrence of the integer overflow. Your program must work on any computer and for any size of integers; so, do not try to assume some specific word size. You have to detect the overflow and prevent that it creates wrong results.

Solutions

Expert Solution

Solution:

(a)

Integer Overflow is a phenomenon where operations on 2 numbers exceeds the maximum (or goes below the minimum) value the integer data type can have. Usually it is thought that integral types are very large and people don't take into account the fact that sum of two numbers can be larger than the range. If size of a signed data type is n bytes, it ranges from -28n-1 to 28n-1-1
So, a short(usually 2 bytes) ranges from -32768 to 32767.

(b)

#include <iostream>
using namespace std;
// Function to check integer overflow
bool isOverflow(long long a, long long b) 
{ 
    // Check if either of them is zero 
    if (a == 0 || b == 0)  
        return false; 
    long long result = a * b;
        // Condition for multiplicative integer overflow 
    if (a == result / b) 
        return false; 
    else
        return true; 
}
//powerof function
int calcPower(long long int x, long long int y) 
{
    long long int result = 1;
    while (y > 0) 
        {
        if (y & 1)
        {
                if (isOverflow(result, x))
                        return -1;
                result *= x;
                }
        y = y >> 1;
        if (isOverflow(x, x))
                return -1;
        x = x * x;
    }
    return result;
}

int main()
{
        long long int i, n = INT_MAX;
        // loop for series, n = 0, 1, 2, .....,INT_MAX
        for(i=0; i<n; i++)
        {
                int res = calcPower(2, i);
                if(res == -1)
                        break;
                // Print till the overflow condition gets true
                cout<<2<<"^"<<i<<" = "<<res<<endl;  
        }       
        return 0;
}

OUTPUT:

If it somehow helps... plz give a like.

Good Luck :).


Related Solutions

write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
c++ Write a program that displays the status of an order. a) Program uses 2 functions...
c++ Write a program that displays the status of an order. a) Program uses 2 functions (in addition to main ()). b) The first function asks the user for the data below and stores the input values in reference parameters. c) Input data from user: # of spools ordered, # of spools in stock, any special shipping & handling charges over and above the $10 rate. d) The second function receives as arguments any values needed to compute and display...
Complete 2a-b 2a) Write a C program which displays the sum of the command line arguments....
Complete 2a-b 2a) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that can be added. Use the file name sum.c for your program. Test your program with sum 5 -10 3 and sum 8 and sum 2b) Write a C program that reads in a file and outputs it in reverse order. Use recursion. Test your program with the file reverse.txt...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and prints the integer, but if something other than an integer is input, the program keeps asking for an integer. Here is a sample session: Input an integer: abc Error: try again. Input an integer: 4a Error: try again. Input an integer: 2.5 Error: try again. Input an integer: 123 The integer is: 123 Hint: the string isdigit method will be useful to solve this...
Provide a detailed explanation of fishers’ theorem
Provide a detailed explanation of fishers’ theorem
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers....
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers. The program should be run from the command prompt, output a text prompt to the screen, and then wait for the user to type in a number followed by the Enter key. (The legitimate range of user input values is any signed integer that can be represented in 32 bits.) After each number is entered, the program should determine and display the following information...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT