Question

In: Computer Science

Write a C++ main program that has the following 5 short independent segments. 1. Show that...

Write a C++ main program that has the following 5 short independent segments.

1. Show that for unsigned int a,b and a>0, b>0, we can get a+b < a

2. Show that for int a,b and a>0, b>0, we can get a+b < 0

3. Show that for int a,b and a 0

4. Show that for double x and x>0 we can get 1. + x = = 1.

5. Show that for double a,b,c in some cases (a+b)+c != (c+b)+a

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <iostream>
using namespace std;

int main() {
  
{// SEGMENT 1
// 1. example
   unsigned int a=4294967295;
   unsigned int b=100;
   cout<<"\na="<<a<<" ; b="<<b<<" ; a+b="<<a+b<<endl;
   cout<<"Hence, a+b can be less than a for some values of unsigned int."<<endl;
}
  
{// SEGMENT 2
  
   // 2. example
   int a=2147483647;
   int b=100;
   cout<<"\na="<<a<<" ; b="<<b<<" ; a+b="<<a+b<<endl;
   cout<<"Hence, a+b can be negative for some positive values of int."<<endl;
}
  
{// SEGMENT 3
  
   // 3. example
   int a=-2147483647;
   int b=-100;
   cout<<"\na="<<a<<" ; b="<<b<<" ; a+b="<<a+b<<endl;
   cout<<"Hence, a+b can be positive for some negative values of int."<<endl;
}

{// SEGMENT 4
  
   // 4. example
   double x=0.0000000007;
   cout<<"\nx="<<x<<" ; 1. + x ="<<1.+x<<endl;
   cout<<"Hence, we can get (1.+x==1.) for insignificant value of double x."<<endl;
}

{// SEGMENT 5
  
   // 5. example
   double a=1;
   double b=1e20;
   double c=-1e20;
  
   double x= (a+b)+c;
   double y= (c+b)+a;
  
   cout<<"\n(a+b)+c = "<<x<<" ; (c+b)+a = "<<y<<endl;
   cout<<"Hence, (a+b)+c != (c+b)+a"<<endl;
}
  
  
   return 0;
}

===========

OUTPUT:


Related Solutions

Write a C++ main program that has the following 5 short independent segments. 6. Show the...
Write a C++ main program that has the following 5 short independent segments. 6. Show the results of the following power function: pow(-2., 3), pow(-2., 3.0) , pow(-2., 3.00000000001) 7. Show the memory size of the following constants 1. , 1.F, 1 , '1' , and "1" 8. Display 1./3. using 20 digits and show the correct and incorrect digits 9. Display all printable characters of the ASCII table in 3 columns: first column: 32-63, second column: 64-95, third column:...
Write a C++ main program that has the following: 9. Display all printable characters of the...
Write a C++ main program that has the following: 9. Display all printable characters of the ASCII table in 3 columns: first column: 32-63, second column: 64-95, third column: 96-127. Each column must include the numeric value and the corresponding character. Following is an example of one of 32 rows in the ASCII table: 33 ! 65 A 97 a
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
For the following program segments, write a program that shows the estimated runtime for each piece....
For the following program segments, write a program that shows the estimated runtime for each piece. Run it on your computer when n=1, 10, 100, 1000, 10000, 100000, 1000000 for ten times each so that you can observe the differences in performance among these segments. Segment1:        for (sum=0, i=1; i<=n; i++)                         sum = sum + i; Segment2:        for (sum=0, i=1; i<=n; i++)                                                 for (j=1; j<=i; j++)                                                             sum++; Segment3:        sum= n * (n+1)/2
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print...
Write a program (in Q0.c) to do the following: In main(), declare an integer x. Print the address of x (using the address-of operator). Pass x as an argument to a function void fooA(int* iptr). (Hint: can you pass x itself directly?) In fooA(int* iptr), print the value of the integer pointed to by iptr, the address pointed to by iptr, and the address of iptr itself. In the main function, following the call to fooA(...) , print the value...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT