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

Let a , b , c be three integer numbers. Write a C++ program with a...
Let a , b , c be three integer numbers. Write a C++ program with a functions void rotate1(int* a,int* b,int* c) void rotate2(int& a,int& b,int& c) such that a -> b , b -> c and c -> a. Thus we use two different approaches (pointers in rotate1 and references in rotate2).
Write a C++ program that displays the current time
Write a C++ program that displays the current time
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
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...
Provide a detailed explanation of fishers’ theorem
Provide a detailed explanation of fishers’ theorem
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...
C++ program that reads a positive integer number from a user and displays all binary numbers...
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number. For the program, you can assume that the input number is a number between 0 and 100. Sample Run 1: Assume that the user typed the following number. 5 This is the correct output of your program. 000 001 010 011 100 101 Sample Run 2: Assume that the user typed the following number. 0 This is the correct...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT