Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- // Function prototype (declaration)
- int add(int, int);
- double doubleadd(double, double);
- float squareroot(float);
- int main()
- {
- int bignum = 2147483646;
- //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
- cout << "Two positive numbers added = ";
- int num = add(bignum , 2 );
- cout << num << endl;
- //3. Show that for int a,b and a<0, b<0, we can get a+b > 0
- int bignegnum = bignum * -1;
- int num2 = add(bignegnum , -10);
- cout << "Two negative numbers added = " << num2 << endl;
- //4. Show that for double x and x>0 we can get 1. + x = = 1.
- double dnum1 = 1.0;
- double dnum2 = .00000000000000000001;
- dnum1 = doubleadd(dnum1, dnum2);
- cout << "Two double numbers added = " << dnum1 << endl;
- //5. Show that for double a,b,c in some cases (a+b)+c != (c+b)+a
- dnum1 = 2000000;
- dnum2 = 1e20;
- double dnum3 = -1e20;
- double dnum4 = (dnum1 + dnum2) + dnum3;
- double dnum5 = (dnum3 + dnum2) + dnum1;
- cout<< "The two different numbers when added in different order: " << dnum4 << " and " << dnum5 <<endl;
- //6. Show the results of the following power function: pow(-2., 3), pow(-2., 3.0) , pow(-2., 3.00000000001)
- double powBase = -2.;
- double powExp = 3;
- double powNum = pow(powBase, powExp);
- cout << " Pow(-2.3,3) = " << powNum << endl;
- powExp = 3.0;
- powNum = pow(powBase, powExp);
- cout << " Pow(-2.,3.0) = " <<powNum << endl;
- powExp = 3.00000000001;
- powNum = pow(powBase, powExp);
- cout << " Pow(-2.,3.00000000001) = " <<powNum << endl;
- //7. Show the memory size of the following constants 1. , 1.F, 1 , '1' , and "1"
- cout<<"Size of (1.) : " <<sizeof(1.)<< endl;
- cout<<"Size of (1.F) : "<<sizeof(1.F) << endl;
- cout<<"Size of (1) : "<<sizeof(1)<< endl;
- cout<<"Size of ('1') : "<<sizeof('1')<< endl;
- cout<<"Size of (\"1\") : "<<sizeof("1")<< endl;
- //8. Display 1./3. using 20 digits and show the correct and incorrect digits
- cout<<"1./3: " << setprecision(20) << 1./3 <<endl;
- /* 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 */
- for (int i = 32; i< 63; i++){
- cout << i << " " << (char)i <<"\t" << (i+32) <<" " << (char)(i+32) << "\t" << (i+64) << " " << (char)(i+64) <<endl;
- }
- //10. Compute sqrt(2.) using your own program for square root function.
- cout<< "Square root of (2.) is " << squareroot(2);
- return 0;
- }
- int add(int a, int b)
- {
- int sum;
- sum = a + b;
- return sum;
- }
- double doubleadd(double a, double b){
- double sum;
- sum = a + b;
- return sum;
- }
- float squareroot(float a)
- {
- float root = a/2.0;
- float temp = 0;
- while (root != temp)
- {
- temp = root;
- root = ( a / temp + temp) / 2;
- }
- return root;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement