In: Computer Science
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
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: