Question

#include <iostream> #include <climits> Using namespace std; Intmain() { Int I; Int j; Cout << “For...

#include <iostream>

#include <climits>

Using namespace std;

Intmain() {

Int I;

Int j;

Cout << “For this compiler: “ << endl;

Cout << “integers are: “ << sizeof(int) << “bytes” << endl;

Cout << “largest integers is “ <<INT_MAX << endl;

Cout << “smallest integers is “ <<INT_MIN << endl;

Cout << “Input two integers values “ << endl;

Cin >> i >> j;

Cout << endl << “You entered the following values: “ << endl;

Cout << “integer “ << i << “ “ << j << endl;

Int result = I * 10;

Cout << “Your number times 10 is “ << result << endl;

Result = i + j;

Cout << “The sum of your numbers is “ << result << endl;

Result = i * j;

Cout<< “ The product of your numbers is “ << result << endl;

Return 0;

}

  1. What is the largest possible value of type int? Explain your answer using the information you read in the background section.
  2. What happens when the result of an operation on values of type int exceeds this value? Explain?
  3. Look up the population of the United States?
  4. Look up the population of the world?
  5. Look up the United States’s national debt.
  6. For which of these would the int data type be a problem. Population of the U.S, Population of the World or US national debt.
  7. Discuss the Comair problem described in the background section. What are the repercussions of such a problem?

Background Summary:

Integers values that are too large or too small may fall outside the allowable range for their data type, leading to undefined behavior that can both reduce the robustness of your code and lead to security vulnerabilities.

Description: Declaring a variable type int allocates a fixed amount of space in memory. Most languages include several integer types, including short, int, long, etc , to allow for less or more storage. The amount of space allocated limits the range of values that can be stored. For example, a 32 bit signed int variable can hold values from -2power31 through 2power31(-1).

Input or mathematical operations such as addition, subtraction, and multiplication may lead to values that are outside of this range. This results in an integer error or overflow, which causes undefined behavior and the resulting value will likely not be what the programmer intended. Integer overflow is a common cause of software errors and vulnerabilities.

RISK HOW CAN IT HAPPEN?

An integer error can lead to unexpected behavior or maybe exploited to cause a program crash, corrupt data, lead to incorrect behavior, or allow the execution of malicious software.

Real World Examples: There is a facebook group called ‘if this group reaches 4,294,967,296 it might cause an integer overflow’. The value is 1 more than the largest number that can fit in a 32 bit unsigned integer. If the number of the members of the group exceeded this number, it might cause an overflow. Whether it will cause an overflow or not depends upon how Facebook is implemented and which language is used. -they might use data types that can hold large numbers. In any case, the chances of an overflow seem remote, as roughly 2/3 people on earth would be required to reach the goal of more than 4 billion members.

2- On Dec 25, 2004, Comair Airline was forced to grounf 1,100flights after it’s flight crew scheduling software crashed. The software used a 16 bit integer (max 32,767) to store the number of crew changes. That number exceeded due to bad weather that month which led to numerous crew reassignments.

[Declaring a variable as type integer allocates a fixed amount of storage.

An ineteger error in C++ causes unexpected behavior]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The output of the above code for i=1 & j=2 :-

For this compiler:
integers are: 4bytes
largest integers is 2147483647
smallest integers is -2147483648
Input two integers values

You entered the following values:
integer 1 2
Your number times 10 is 10
The sum of your numbers is 3
The product of your numbers is 2

1.What is the largest possible value of type int? Explain your answer using the information you read in the background section.

Answer:-

The largest possible value of type int is 2147483647 . Size of 1 byte is 8 bits and size of integer is 4 bytes (=32bits). The largest possible value of type int is 2147483647 . Because int is a signed type, 1 bit is used for sign. 2^(32-1)=2147483647.

2. What happens when you exceed the largest integer value? Explain.

Answer:-

When the result of an operation on values of type int exceeds this value i.e. 2147483647, the problem of integer overflow occurs.The overflow results in negative number .

If we input i=2147483647 , j=1 then i+j will result in integer overflow and the output is -2147483648.

3.For which of the above would the integer data type be a problem?

Answer:-  Population of the world, National debt.

The US Population in 2020 is 32,82,00,000‬ which is less than the maximum value of type int .So there isn't any problem.

The world population is 7,59,43,00,000‬ which is greater than maximum value of type int . So we cannot assign this value to type int.

The US national debt is $24.95 trillion which is greater than the maximum value of type int . So we cannot assign this vale to type int.

4. Discuss the Comair problem described above. What are the repercussions of such a problem?

Answer:-

In case of comair flight , the number was greater than the 16 bit int data type (i.e.  32,767) which causes an integer overflow and hence giving wrong outputs. .Using 32 bit integer (max 2,147,483,647) to store crew changes ensure the safety of software .Ensure that overflow does not occur.

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <climits> Using namespace std; Intmain() { Int I; Int j; Cout << “For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int...

    #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int lab[4]; myPtr = new int *[row]; for(int i = 0; i < row; i++) myPtr[i] = new int[lab[i]]; for(int i = 0; i<row ; i++) if(myPtr[i] == 0) cout<<"empty"; return myPtr; } void getinput(int ID,int &Station,int &labnumb) { cout<<" Enter your ID number: "<<endl; cin>>ID; cout<<" Enter your station number: "<<endl; cin>>Station; cout<<" Enter your lab number: "<<endl; cin>>labnumb; return; } void logout(int ID,int...

  • Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

    Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k;    Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;    Magic(2,4); cout<<"i, j, k and...

  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating...

    #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating odd numbers, init to 0    int sumEven = 0; // For accumulating even numbers, init to 0    int upperbound; // Sum from 1 to this upperbound    // Prompt user for an upperbound    cout << "Enter the upperbound: ";    cin >> upperbound;    // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound    int number =...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; }...

    C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; } bool checkinventoryprice(float price) {    return price > 0; } void endProgram() {    system("pause");    exit(0); } void errorID(string str) {    cout << "Invalid Id!!! " << str << " should be greater than 0" << endl; } void errorPrice(string str) {    cout << "Invalid Price!!!" << str << " should be greater than 0" << endl; } int inputId() {...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT